diff --git a/.crow/backfill-abi-classification.yaml b/.crow/backfill-abi-classification.yaml index aeab591..6eef94d 100644 --- a/.crow/backfill-abi-classification.yaml +++ b/.crow/backfill-abi-classification.yaml @@ -74,6 +74,7 @@ steps: - git clone -q https://pat-s:$$REPO_RO_TOKEN@git.devxy.io/devxy/build-cran-binaries.git . - mkdir -p /tmp/R-pkgs - /opt/R/4.5.3/bin/Rscript local/install-bincraft.R + - UVR_R_BIN=/opt/R/4.5.3/bin/R local/uvr-install.sh RPostgres - | set -eu LIMIT="" diff --git a/.crow/backfill-flat-safety.yaml b/.crow/backfill-flat-safety.yaml index 0db0735..85c9462 100644 --- a/.crow/backfill-flat-safety.yaml +++ b/.crow/backfill-flat-safety.yaml @@ -140,12 +140,11 @@ steps: from_secret: PGPASS REPO_RO_TOKEN: from_secret: REPO_RO_TOKEN - # uvr resolves bincraft's dependencies through the GitHub API, including - # the pat-s/cranlike@s3 fork. Unauthenticated it is rate limited, and the - # failure is not loud: uvr warns, falls back to the registry copy of - # cranlike, and the index write then dies on `unused argument (built = - # built)` because upstream has no such parameter. Intermittent by nature, - # since it depends on the rate-limit window. + # bincraft reads the DESCRIPTION of every new or replaced tarball from + # the CRAN mirror on raw.githubusercontent.com, and authenticates with + # this token when it is set. A slot whose published index cannot be read + # is re-read in full, ~25k requests, which the anonymous rate limit would + # cut short. GITHUB_PAT: from_secret: GITHUB_PAT GIT_USER: pat-s @@ -156,6 +155,7 @@ steps: - git clone -q https://pat-s:$$REPO_RO_TOKEN@git.devxy.io/devxy/build-cran-binaries.git . - mkdir -p /tmp/R-pkgs - /opt/R/$R_VERSION/bin/Rscript local/install-bincraft.R + - UVR_R_BIN=/opt/R/$R_VERSION/bin/R local/uvr-install.sh RPostgres - | set -eu LIMIT="" diff --git a/.crow/build-all-versions-install-deps.yaml b/.crow/build-all-versions-install-deps.yaml index 2a6d9c2..c092c7a 100644 --- a/.crow/build-all-versions-install-deps.yaml +++ b/.crow/build-all-versions-install-deps.yaml @@ -36,6 +36,13 @@ variables: - 4.5.3 - 4.4.3 default: 4.5.3 + SEED_FROM: + # Only for bootstrapping a new slot. Copies the pure-R binaries of these + # slots (same OS family and arch, same primary R minor; see + # local/seed-pure-r.R) before the build list is computed, so build-all only + # compiles what actually needs compiling. Space-separated, first wins. + description: 'Existing slots to copy pure-R binaries from, e.g. "alpine324 alpine323". Empty: no seeding.' + default: '' when: - event: manual @@ -86,8 +93,9 @@ steps: # Pin the same bincraft version the build steps use, so the precomputed # snapshot and the per-agent library stay consistent across the pipeline. - /opt/R/$R_VERSION/bin/Rscript local/install-bincraft.R - - UVR_R_BIN=/opt/R/$R_VERSION/bin/R local/uvr-install.sh RPostgres s3fs data.table future jsonlite + - UVR_R_BIN=/opt/R/$R_VERSION/bin/R local/uvr-install.sh RPostgres s3fs data.table future jsonlite purrr - /opt/R/$R_VERSION/bin/R -q -e 'packageVersion("bincraft")' + - if [ -n "${SEED_FROM}" ]; then /opt/R/$R_VERSION/bin/Rscript local/seed-pure-r.R ${SEED_FROM}; fi - /opt/R/$R_VERSION/bin/R -q -e "source('local/packages-to-build.R'); saveRDS(pkgs, '/mnt/cache/packages/pkgs_to_build.rds'); saveRDS(pkgs[r_minor_sensitive == TRUE], '/mnt/cache/packages/r_minor_sensitive_pkgs.rds'); sprintf('Precomputed %s package versions (%s r-minor-sensitive)', nrow(pkgs), nrow(pkgs[r_minor_sensitive == TRUE]))" backend_options: docker: diff --git a/.crow/build-all-versions.yaml b/.crow/build-all-versions.yaml index 9a768d1..ae5cd89 100644 --- a/.crow/build-all-versions.yaml +++ b/.crow/build-all-versions.yaml @@ -136,10 +136,18 @@ steps: R_LIBS_USER: /mnt/cache/R-pkgs CCACHE_DIR: /mnt/cache/ccache NCPUS: 2 + # Whole hours the build passes below may use, measured from the step's + # start. The repo timeout is 50h; the remainder is left for archiving the + # missed packages, so a long run ends cleanly instead of being killed. + BUILD_BUDGET_HOURS: 44 volumes: - ${ARCH}-binaries-r-dep-cache-${OS}-${OS_VERSION//./}:/mnt/cache commands: - git clone -q https://pat-s:$$REPO_RO_TOKEN@git.devxy.io/devxy/build-cran-binaries.git . + - . local/compiler-cache.sh + # One deadline shared by every build pass in this step, read by + # local/build-all.R and local/build-newer-r.R before each package. + - export BUILD_DEADLINE=$(( $(date +%s) + BUILD_BUDGET_HOURS * 3600 )) - mkdir -p /mnt/cache/uvr/cache /mnt/cache/uvr/packages /mnt/cache/R-pkgs /mnt/cache/ccache /mnt/cache/packages # The primary pass must not rely on build-all-versions-install-deps having # run on *this* agent: depends_on only orders the steps, but the cache @@ -152,7 +160,7 @@ steps: # The dependency-prep job may run on another agent whose cache volume is # not visible here. Install the packages needed to recompute the snapshot # and query build metadata in every build job. - - UVR_R_BIN=/opt/R/$R_VERSION/bin/R local/uvr-install.sh RPostgres s3fs data.table future jsonlite + - UVR_R_BIN=/opt/R/$R_VERSION/bin/R local/uvr-install.sh RPostgres s3fs data.table future jsonlite purrr - XVFB=$(command -v xwfb-run 2>/dev/null || command -v xvfb-run); XVFB_ARGS="-a"; if command -v xwfb-run >/dev/null 2>&1; then dnf install -y -q weston 2>/dev/null; XVFB_ARGS="-c weston"; fi - $XVFB $XVFB_ARGS -n $SPLIT_INDEX -- /opt/R/$R_VERSION/bin/Rscript local/build-all.R $SPLIT_INTO $SPLIT_INDEX $NCPUS 2>&1 - | @@ -193,8 +201,8 @@ steps: from_secret: B2_S3_ACCESS_KEY B2_S3_SECRET_KEY: from_secret: B2_S3_SECRET_KEY - # The per-minor index reads the ABI classifier cache and the flat-object - # safety table, and aborts without the database password. + # A per-minor index reads the ABI classifier cache and the flat-object + # safety table from the build database, and is not republished without it. PGPASS: from_secret: PGPASS R_LIBS_USER: /mnt/cache/R-pkgs @@ -204,12 +212,15 @@ steps: - | CODENAME=$(/opt/R/$R_VERSION/bin/Rscript -e "cat(bincraft::set_codename(NULL))") /opt/R/$R_VERSION/bin/R -q -e "bincraft::upload_package_index(codename = '$CODENAME', s3_endpoint = 'https://s3.eu-central-003.backblazeb2.com', s3_region = 'eu-central-003', s3_bucket = 'devxy-rpkgs-binaries', s3_access_key_id = Sys.getenv('B2_S3_ACCESS_KEY'), s3_secret_access_key = Sys.getenv('B2_S3_SECRET_KEY'))" - failed="" + FAILED_MINORS="" for RBIN in /opt/R/[0-9]*/bin/R; do RMINOR=$(basename "$(dirname "$(dirname "$RBIN")")" | cut -d. -f1-2) - /opt/R/$R_VERSION/bin/R -q -e "bincraft::upload_package_index(codename = '$CODENAME', r_minor = '$RMINOR', s3_endpoint = 'https://s3.eu-central-003.backblazeb2.com', s3_region = 'eu-central-003', s3_bucket = 'devxy-rpkgs-binaries', s3_access_key_id = Sys.getenv('B2_S3_ACCESS_KEY'), s3_secret_access_key = Sys.getenv('B2_S3_SECRET_KEY'), metadata_db_host = 'r-binaries.devxy.io', metadata_db_name = 'build_metadata', metadata_db_port = 15432L, metadata_db_user = 'rpkgs', metadata_db_password = Sys.getenv('PGPASS'), metadata_db_sslmode = 'require')" || failed="$failed $RMINOR" + /opt/R/$R_VERSION/bin/R -q -e "bincraft::upload_package_index(codename = '$CODENAME', r_minor = '$RMINOR', s3_endpoint = 'https://s3.eu-central-003.backblazeb2.com', s3_region = 'eu-central-003', s3_bucket = 'devxy-rpkgs-binaries', s3_access_key_id = Sys.getenv('B2_S3_ACCESS_KEY'), s3_secret_access_key = Sys.getenv('B2_S3_SECRET_KEY'), metadata_db_host = 'r-binaries.devxy.io', metadata_db_name = 'build_metadata', metadata_db_port = 15432L, metadata_db_user = 'rpkgs', metadata_db_password = Sys.getenv('PGPASS'), metadata_db_sslmode = 'verify-full')" || FAILED_MINORS="$FAILED_MINORS $RMINOR" done - if [ -n "$failed" ]; then echo "Per-minor reindex failed for:$failed" >&2; exit 1; fi + if [ -n "$FAILED_MINORS" ]; then + echo "Could not republish the per-minor index for:$FAILED_MINORS" >&2 + exit 1 + fi backend_options: docker: resources: diff --git a/.crow/download-stats.yaml b/.crow/download-stats.yaml new file mode 100644 index 0000000..4840ad3 --- /dev/null +++ b/.crow/download-stats.yaml @@ -0,0 +1,79 @@ +# Collect CDN access logs and publish the public download statistics. +# +# Each run reads the last 70 hours of cran-rpkgs access logs from Bunny into +# `download_stats.events` (build metadata Postgres), then rewrites +# https://cran.rpkgs.com/stats/downloads.json, which the rpkgs.com /stats page +# reads. Bunny keeps logs for only three days, so a run every hour leaves room +# for two days of failed runs before anything is lost. Re-reading the window is +# safe: events are keyed by Bunny's request id. +# +# The classification rules and what is excluded are documented in +# `stats/README.md`. +# +# Register the `download-stats` cron (hourly, `0 * * * *`) in the crow UI, or +# run manually: +# crow pipeline create --branch main --var download_stats=true devxy/build-cran-binaries +# +# Secrets: +# STATS_CLIENT_SALT HMAC key for the client hash. Rotating it resets +# distinct-client counts, so keep it stable. +# STATS_OWN_NETWORKS Comma-separated CIDRs of our own CI runners. +# Both live in OpenBao at development/agent-dev/rpkgs-download-stats. +variables: + download_stats: + description: 'Collect and publish download statistics. Also gates this pipeline.' + options: + - 'true' + - 'false' + default: 'false' + +when: + - event: manual + evaluate: 'download_stats == "true"' + - event: cron + cron: download-stats + +skip_clone: true + +labels: + group: rpkgs-amd64 + +steps: + - name: 'Collect and publish download stats' + image: oven/bun:1.4.5-alpine + pull: true + environment: + BUNNYNET_API_KEY: + from_secret: BUNNYNET_API_KEY + PGPASS: + from_secret: PGPASS + B2_S3_ACCESS_KEY: + from_secret: B2_S3_ACCESS_KEY + B2_S3_SECRET_KEY: + from_secret: B2_S3_SECRET_KEY + STATS_CLIENT_SALT: + from_secret: STATS_CLIENT_SALT + STATS_OWN_NETWORKS: + from_secret: STATS_OWN_NETWORKS + REPO_RO_TOKEN: + from_secret: REPO_RO_TOKEN + commands: + - apk add --no-cache -q git + - git clone -q --depth 1 --branch "$CI_COMMIT_BRANCH" https://pat-s:$$REPO_RO_TOKEN@git.devxy.io/devxy/build-cran-binaries.git . + - bun test stats + - bun stats/collect.ts + - bun stats/publish.ts + backend_options: + kubernetes: + resources: + requests: + memory: 256Mi + cpu: 250m + limits: + memory: 1Gi + cpu: 1000m + tolerations: + - key: 'CI' + operator: 'Equal' + value: 'true' + effect: 'NoSchedule' diff --git a/.crow/process-updates.yaml b/.crow/process-updates.yaml index 5748cdb..e4ef547 100644 --- a/.crow/process-updates.yaml +++ b/.crow/process-updates.yaml @@ -179,6 +179,8 @@ steps: R_LIBS_USER: /mnt/cache/R-pkgs R_VERSION: ${R_VERSION} CCACHE_DIR: /mnt/cache/ccache + # parallel make within one package build, matched to the step's CPU limit + MAKE_JOBS: 3 PLATFORM: ${OS} OS_ID: ${OS_ID} ARCH: ${ARCH} @@ -186,15 +188,16 @@ steps: INTERVAL: lubridate::interval(lubridate::today() - 6, lubridate::today() - 3) commands: - git clone -q https://pat-s:$$REPO_RO_TOKEN@git.devxy.io/devxy/build-cran-binaries.git . + - . local/compiler-cache.sh - rm -rf /mnt/cache/R-pkgs/00LOCK-* /mnt/cache/R-pkgs/bincraft - mkdir -p /mnt/cache/uvr/cache /mnt/cache/uvr/packages /mnt/cache/R-pkgs /mnt/cache/ccache /mnt/cache/packages - /opt/R/$R_VERSION/bin/Rscript local/install-bincraft.R - - UVR_R_BIN=/opt/R/$R_VERSION/bin/R local/uvr-install.sh RPostgres + - UVR_R_BIN=/opt/R/$R_VERSION/bin/R local/uvr-install.sh RPostgres progressr lubridate - /opt/R/$R_VERSION/bin/R -q -e 'packageVersion("bincraft")' # rhel-10 ships xwfb-run (Xwayland) instead of xvfb-run; prefer it and start weston, else fall back to xvfb-run - XVFB=$(command -v xwfb-run 2>/dev/null || command -v xvfb-run); XVFB_ARGS="-a"; if command -v xwfb-run >/dev/null 2>&1; then dnf install -y -q weston 2>/dev/null; XVFB_ARGS="-c weston"; fi # options(future.globals.onReference = NULL): for some reason s3fs::file_delete() throws 'Error: Detected a non-exportable reference ('externalptr') in one of the globals ('FUN' of class 'function') used in the future expression' otherwise - - $XVFB $XVFB_ARGS -- /opt/R/$R_VERSION/bin/R -q -e "options(crayon.enabled = TRUE, Ncpus = 4, future.globals.onReference = 'error', repos = structure(c(getOption('repos'),INLA='https://inla.r-inla-download.org/R/stable'))); progressr::handlers('cli'); progressr::handlers(global = TRUE); options(future.globals.onReference = NULL); bincraft::process_cran_updates(interval = $INTERVAL, platform = '${OS}', process_updated = TRUE, process_new = ${PROCESS_NEW}, process_removed = TRUE, patches = 'local/patches', r_minor_detection = 'classifier',s3_endpoint = 'https://s3.eu-central-003.backblazeb2.com', s3_region = 'eu-central-003', s3_bucket = 'devxy-rpkgs-binaries', s3_access_key_id = Sys.getenv('B2_S3_ACCESS_KEY'), s3_secret_access_key = Sys.getenv('B2_S3_SECRET_KEY'), metadata_db_host = 'r-binaries.devxy.io', metadata_db_name = 'build_metadata', metadata_db_table = 'single_builds', metadata_db_user = 'rpkgs', metadata_db_password = Sys.getenv('PGPASS'), metadata_db_sslmode = 'require', metadata_db_port = 15432, archive = TRUE, upload = TRUE, store_build_metadata = TRUE)" + - $XVFB $XVFB_ARGS -- /opt/R/$R_VERSION/bin/R -q -e "options(crayon.enabled = TRUE, Ncpus = 4, future.globals.onReference = 'error', repos = structure(c(getOption('repos'),INLA='https://inla.r-inla-download.org/R/stable'))); progressr::handlers('cli'); progressr::handlers(global = TRUE); options(future.globals.onReference = NULL); bincraft::process_cran_updates(interval = $INTERVAL, platform = '${OS}', process_updated = TRUE, process_new = ${PROCESS_NEW}, process_removed = TRUE, patches = 'local/patches', r_minor_detection = 'classifier',s3_endpoint = 'https://s3.eu-central-003.backblazeb2.com', s3_region = 'eu-central-003', s3_bucket = 'devxy-rpkgs-binaries', s3_access_key_id = Sys.getenv('B2_S3_ACCESS_KEY'), s3_secret_access_key = Sys.getenv('B2_S3_SECRET_KEY'), metadata_db_host = 'r-binaries.devxy.io', metadata_db_name = 'build_metadata', metadata_db_table = 'single_builds', metadata_db_user = 'rpkgs', metadata_db_password = Sys.getenv('PGPASS'), metadata_db_sslmode = 'verify-full', metadata_db_port = 15432, archive = TRUE, upload = TRUE, store_build_metadata = TRUE)" - | PRIMARY_MINOR=$(echo "$R_VERSION" | cut -d. -f1-2) for RBIN in /opt/R/[0-9]*/bin/R; do @@ -205,19 +208,22 @@ steps: LIB="/mnt/cache/R-pkgs-$RMINOR" mkdir -p "$LIB" R_LIBS_USER="$LIB" "$(dirname "$RBIN")/Rscript" local/install-bincraft.R || true - R_LIBS_USER="$LIB" UVR_R_BIN="$RBIN" local/uvr-install.sh RPostgres || true - R_LIBS_USER="$LIB" $XVFB $XVFB_ARGS -- "$(dirname "$RBIN")/R" -q -e "options(crayon.enabled = TRUE, Ncpus = 4, future.globals.onReference = NULL); bincraft::process_cran_updates(interval = $INTERVAL, platform = '${OS}', process_updated = TRUE, process_new = FALSE, process_removed = FALSE, patches = 'local/patches', r_minor_detection = 'classifier',r_minor_sensitive_only = TRUE, s3_endpoint = 'https://s3.eu-central-003.backblazeb2.com', s3_region = 'eu-central-003', s3_bucket = 'devxy-rpkgs-binaries', s3_access_key_id = Sys.getenv('B2_S3_ACCESS_KEY'), s3_secret_access_key = Sys.getenv('B2_S3_SECRET_KEY'), metadata_db_host = 'r-binaries.devxy.io', metadata_db_name = 'build_metadata', metadata_db_table = 'single_builds', metadata_db_user = 'rpkgs', metadata_db_password = Sys.getenv('PGPASS'), metadata_db_sslmode = 'require', metadata_db_port = 15432, archive = TRUE, upload = TRUE, store_build_metadata = TRUE)" || true + R_LIBS_USER="$LIB" UVR_R_BIN="$RBIN" local/uvr-install.sh RPostgres lubridate || true + R_LIBS_USER="$LIB" $XVFB $XVFB_ARGS -- "$(dirname "$RBIN")/R" -q -e "options(crayon.enabled = TRUE, Ncpus = 4, future.globals.onReference = NULL); bincraft::process_cran_updates(interval = $INTERVAL, platform = '${OS}', process_updated = TRUE, process_new = FALSE, process_removed = FALSE, patches = 'local/patches', r_minor_detection = 'classifier',r_minor_sensitive_only = TRUE, s3_endpoint = 'https://s3.eu-central-003.backblazeb2.com', s3_region = 'eu-central-003', s3_bucket = 'devxy-rpkgs-binaries', s3_access_key_id = Sys.getenv('B2_S3_ACCESS_KEY'), s3_secret_access_key = Sys.getenv('B2_S3_SECRET_KEY'), metadata_db_host = 'r-binaries.devxy.io', metadata_db_name = 'build_metadata', metadata_db_table = 'single_builds', metadata_db_user = 'rpkgs', metadata_db_password = Sys.getenv('PGPASS'), metadata_db_sslmode = 'verify-full', metadata_db_port = 15432, archive = TRUE, upload = TRUE, store_build_metadata = TRUE)" || true # Also build current packages whose required dependency tree needs a newer R. R_LIBS_USER="$LIB" $XVFB $XVFB_ARGS -- "$(dirname "$RBIN")/Rscript" local/build-newer-r.R "$R_VERSION" done - /opt/R/$R_VERSION/bin/R -q -e 'library(bincraft); upload_package_index(codename = "${OS_ID}", s3_endpoint = "https://s3.eu-central-003.backblazeb2.com", s3_region = "eu-central-003", s3_bucket = "devxy-rpkgs-binaries", s3_access_key_id = Sys.getenv("B2_S3_ACCESS_KEY"), s3_secret_access_key = Sys.getenv("B2_S3_SECRET_KEY"))' - | - failed="" + FAILED_MINORS="" for RBIN in /opt/R/[0-9]*/bin/R; do RMINOR=$(basename "$(dirname "$(dirname "$RBIN")")" | cut -d. -f1-2) - /opt/R/$R_VERSION/bin/R -q -e "library(bincraft); upload_package_index(codename = '${OS_ID}', r_minor = '$RMINOR', s3_endpoint = 'https://s3.eu-central-003.backblazeb2.com', s3_region = 'eu-central-003', s3_bucket = 'devxy-rpkgs-binaries', s3_access_key_id = Sys.getenv('B2_S3_ACCESS_KEY'), s3_secret_access_key = Sys.getenv('B2_S3_SECRET_KEY'), metadata_db_host = 'r-binaries.devxy.io', metadata_db_name = 'build_metadata', metadata_db_port = 15432L, metadata_db_user = 'rpkgs', metadata_db_password = Sys.getenv('PGPASS'), metadata_db_sslmode = 'require')" || failed="$failed $RMINOR" + /opt/R/$R_VERSION/bin/R -q -e "library(bincraft); upload_package_index(codename = '${OS_ID}', r_minor = '$RMINOR', s3_endpoint = 'https://s3.eu-central-003.backblazeb2.com', s3_region = 'eu-central-003', s3_bucket = 'devxy-rpkgs-binaries', s3_access_key_id = Sys.getenv('B2_S3_ACCESS_KEY'), s3_secret_access_key = Sys.getenv('B2_S3_SECRET_KEY'), metadata_db_host = 'r-binaries.devxy.io', metadata_db_name = 'build_metadata', metadata_db_port = 15432L, metadata_db_user = 'rpkgs', metadata_db_password = Sys.getenv('PGPASS'), metadata_db_sslmode = 'verify-full')" || FAILED_MINORS="$FAILED_MINORS $RMINOR" done - if [ -n "$failed" ]; then echo "Per-minor reindex failed for:$failed" >&2; exit 1; fi + if [ -n "$FAILED_MINORS" ]; then + echo "Could not republish the per-minor index for:$FAILED_MINORS" >&2 + exit 1 + fi backend_options: docker: resources: @@ -244,3 +250,7 @@ steps: commands: - apk add --no-cache -q bash curl - bash scripts/purge_cdn_cache.sh "$BUNNYNET_API_KEY" "$ARCH" "$OS_ID" "$SUBDOMAIN1" "$SUBDOMAIN2" "$SUBDOMAIN3" + # Run it even when a per-minor re-index above failed: the generic index + # and the objects were still replaced, and a stale edge keeps them hidden. + when: + - status: [success, failure] diff --git a/.crow/prune-stale-flat-binaries.yaml b/.crow/prune-stale-flat-binaries.yaml index 11ec6c0..b010914 100644 --- a/.crow/prune-stale-flat-binaries.yaml +++ b/.crow/prune-stale-flat-binaries.yaml @@ -167,6 +167,7 @@ steps: - git clone -q https://pat-s:$$REPO_RO_TOKEN@git.devxy.io/devxy/build-cran-binaries.git . - mkdir -p /tmp/R-pkgs - /opt/R/4.5.3/bin/Rscript local/install-bincraft.R + - UVR_R_BIN=/opt/R/4.5.3/bin/R local/uvr-install.sh RPostgres - | set -eu APPLY="" @@ -208,13 +209,20 @@ steps: echo "Dry run: nothing was deleted, so there is nothing to re-index." exit 0 fi + # /tmp is container-local, so the library installed by the prune step is gone here. + mkdir -p /tmp/R-pkgs + /opt/R/4.5.3/bin/Rscript local/install-bincraft.R + UVR_R_BIN=/opt/R/4.5.3/bin/R local/uvr-install.sh RPostgres /opt/R/4.5.3/bin/R -q -e 'library(bincraft); upload_package_index(s3_endpoint = "https://s3.eu-central-003.backblazeb2.com", s3_region = "eu-central-003", s3_bucket = "devxy-rpkgs-binaries", s3_access_key_id = Sys.getenv("B2_S3_ACCESS_KEY"), s3_secret_access_key = Sys.getenv("B2_S3_SECRET_KEY"))' - failed="" + FAILED_MINORS="" for RBIN in /opt/R/[0-9]*/bin/R; do RMINOR=$(basename "$(dirname "$(dirname "$RBIN")")" | cut -d. -f1-2) - /opt/R/4.5.3/bin/R -q -e "library(bincraft); upload_package_index(r_minor = '$RMINOR', s3_endpoint = 'https://s3.eu-central-003.backblazeb2.com', s3_region = 'eu-central-003', s3_bucket = 'devxy-rpkgs-binaries', s3_access_key_id = Sys.getenv('B2_S3_ACCESS_KEY'), s3_secret_access_key = Sys.getenv('B2_S3_SECRET_KEY'), metadata_db_host = 'r-binaries.devxy.io', metadata_db_name = 'build_metadata', metadata_db_port = 15432L, metadata_db_user = 'rpkgs', metadata_db_password = Sys.getenv('PGPASS'), metadata_db_sslmode = 'require')" || failed="$failed $RMINOR" + /opt/R/4.5.3/bin/R -q -e "library(bincraft); upload_package_index(r_minor = '$RMINOR', s3_endpoint = 'https://s3.eu-central-003.backblazeb2.com', s3_region = 'eu-central-003', s3_bucket = 'devxy-rpkgs-binaries', s3_access_key_id = Sys.getenv('B2_S3_ACCESS_KEY'), s3_secret_access_key = Sys.getenv('B2_S3_SECRET_KEY'), metadata_db_host = 'r-binaries.devxy.io', metadata_db_name = 'build_metadata', metadata_db_port = 15432L, metadata_db_user = 'rpkgs', metadata_db_password = Sys.getenv('PGPASS'), metadata_db_sslmode = 'verify-full')" || FAILED_MINORS="$FAILED_MINORS $RMINOR" done - if [ -n "$failed" ]; then echo "Per-minor reindex failed for:$failed" >&2; exit 1; fi + if [ -n "$FAILED_MINORS" ]; then + echo "Could not republish the per-minor index for:$FAILED_MINORS" >&2 + exit 1 + fi backend_options: docker: resources: @@ -242,3 +250,7 @@ steps: fi apk add --no-cache -q bash curl jq bash scripts/purge_cdn_zone.sh "$BUNNYNET_API_KEY" $BUNNY_PULLZONES + # Run it even when a per-minor re-index above failed: the generic index + # and the objects were still replaced, and a stale edge keeps them hidden. + when: + - status: [success, failure] diff --git a/.crow/rebuild-abi-gap.yaml b/.crow/rebuild-abi-gap.yaml index 71ade22..afab781 100644 --- a/.crow/rebuild-abi-gap.yaml +++ b/.crow/rebuild-abi-gap.yaml @@ -193,6 +193,7 @@ steps: export R_LIBS_USER="$$LIB" rm -rf "$$LIB"/00LOCK-* "$$RBIN/Rscript" local/install-bincraft.R + UVR_R_BIN="$$RBIN/R" local/uvr-install.sh RPostgres rc=0 for spec in $$ABI_PACKAGES; do pkg=$${spec%@*} diff --git a/.crow/reindex.yaml b/.crow/reindex.yaml index 082490c..bb8508f 100644 --- a/.crow/reindex.yaml +++ b/.crow/reindex.yaml @@ -141,12 +141,11 @@ steps: from_secret: PGPASS REPO_RO_TOKEN: from_secret: REPO_RO_TOKEN - # uvr resolves bincraft's dependencies through the GitHub API, including - # the pat-s/cranlike@s3 fork. Unauthenticated it is rate limited, and the - # failure is not loud: uvr warns, falls back to the registry copy of - # cranlike, and the index write then dies on `unused argument (built = - # built)` because upstream has no such parameter. Intermittent by nature, - # since it depends on the rate-limit window. + # bincraft reads the DESCRIPTION of every new or replaced tarball from + # the CRAN mirror on raw.githubusercontent.com, and authenticates with + # this token when it is set. A slot whose published index cannot be read + # is re-read in full, ~25k requests, which the anonymous rate limit would + # cut short. GITHUB_PAT: from_secret: GITHUB_PAT GIT_USER: pat-s @@ -159,15 +158,19 @@ steps: - mkdir -p /mnt/cache/R-pkgs - rm -rf /mnt/cache/R-pkgs/00LOCK-* - /opt/R/$R_VERSION/bin/Rscript local/install-bincraft.R + - UVR_R_BIN=/opt/R/$R_VERSION/bin/R local/uvr-install.sh RPostgres # The codename is detected from the image's /etc/os-release. - /opt/R/$R_VERSION/bin/R -q -e 'library(bincraft); upload_package_index(s3_endpoint = "https://s3.eu-central-003.backblazeb2.com", s3_region = "eu-central-003", s3_bucket = "devxy-rpkgs-binaries", s3_access_key_id = Sys.getenv("B2_S3_ACCESS_KEY"), s3_secret_access_key = Sys.getenv("B2_S3_SECRET_KEY"))' - | - failed="" + FAILED_MINORS="" for RBIN in /opt/R/[0-9]*/bin/R; do RMINOR=$(basename "$(dirname "$(dirname "$RBIN")")" | cut -d. -f1-2) - /opt/R/$R_VERSION/bin/R -q -e "library(bincraft); upload_package_index(r_minor = '$RMINOR', s3_endpoint = 'https://s3.eu-central-003.backblazeb2.com', s3_region = 'eu-central-003', s3_bucket = 'devxy-rpkgs-binaries', s3_access_key_id = Sys.getenv('B2_S3_ACCESS_KEY'), s3_secret_access_key = Sys.getenv('B2_S3_SECRET_KEY'), metadata_db_host = 'r-binaries.devxy.io', metadata_db_name = 'build_metadata', metadata_db_port = 15432L, metadata_db_user = 'rpkgs', metadata_db_password = Sys.getenv('PGPASS'), metadata_db_sslmode = 'require')" || failed="$failed $RMINOR" + /opt/R/$R_VERSION/bin/R -q -e "library(bincraft); upload_package_index(r_minor = '$RMINOR', s3_endpoint = 'https://s3.eu-central-003.backblazeb2.com', s3_region = 'eu-central-003', s3_bucket = 'devxy-rpkgs-binaries', s3_access_key_id = Sys.getenv('B2_S3_ACCESS_KEY'), s3_secret_access_key = Sys.getenv('B2_S3_SECRET_KEY'), metadata_db_host = 'r-binaries.devxy.io', metadata_db_name = 'build_metadata', metadata_db_port = 15432L, metadata_db_user = 'rpkgs', metadata_db_password = Sys.getenv('PGPASS'), metadata_db_sslmode = 'verify-full')" || FAILED_MINORS="$FAILED_MINORS $RMINOR" done - if [ -n "$failed" ]; then echo "Per-minor reindex failed for:$failed" >&2; exit 1; fi + if [ -n "$FAILED_MINORS" ]; then + echo "Could not republish the per-minor index for:$FAILED_MINORS" >&2 + exit 1 + fi - name: Purge CDN cache image: reg.devxy.io/docker.io/library/alpine:3.24 diff --git a/.crow/repair-built-stamp.yaml b/.crow/repair-built-stamp.yaml index cc09586..fe499df 100644 --- a/.crow/repair-built-stamp.yaml +++ b/.crow/repair-built-stamp.yaml @@ -3,8 +3,8 @@ # # uvr matches the stamp's platform triple plus R minor to decide binary vs # source, so an unusable triple turns a whole slot source-only. See -# local/repair-built-stamp.R for why this patches PACKAGES.db in place instead -# of forcing a full reparse. +# local/repair-built-stamp.R for why this patches the published PACKAGES.rds in +# place instead of forcing a full reparse. # # Run with `dry_run: true` first: it reports how many entries are broken per # slot and changes nothing. Pick the R version the slot should advertise, which diff --git a/.crow/repair-unsafe-flat-objects.yaml b/.crow/repair-unsafe-flat-objects.yaml index e0625c0..d11084a 100644 --- a/.crow/repair-unsafe-flat-objects.yaml +++ b/.crow/repair-unsafe-flat-objects.yaml @@ -144,6 +144,7 @@ steps: - git clone -q https://pat-s:$$REPO_RO_TOKEN@git.devxy.io/devxy/build-cran-binaries.git . - mkdir -p /tmp/R-pkgs - /opt/R/$R_VERSION/bin/Rscript local/install-bincraft.R + - UVR_R_BIN=/opt/R/$R_VERSION/bin/R local/uvr-install.sh RPostgres - | set -eu CODENAME=$(/opt/R/$R_VERSION/bin/Rscript -e 'cat(bincraft::set_codename(NULL))') @@ -158,6 +159,7 @@ steps: LIB="/tmp/R-pkgs-$$RMINOR" mkdir -p "$$LIB" R_LIBS_USER="$$LIB" "$(dirname "$$RBIN")/Rscript" local/install-bincraft.R || true + R_LIBS_USER="$$LIB" UVR_R_BIN="$$RBIN" local/uvr-install.sh RPostgres || true while read -r pkg ver; do [ -z "$$pkg" ] && continue R_LIBS_USER="$$LIB" "$(dirname "$$RBIN")/Rscript" \ diff --git a/.crow/weekly-audit-missing.yaml b/.crow/weekly-audit-missing.yaml index 2796a47..7072c65 100644 --- a/.crow/weekly-audit-missing.yaml +++ b/.crow/weekly-audit-missing.yaml @@ -137,7 +137,7 @@ steps: - mkdir -p /mnt/cache/packages /mnt/cache/R-pkgs - rm -rf /mnt/cache/R-pkgs/00LOCK-* - /opt/R/$R_VERSION/bin/Rscript local/install-bincraft.R - - UVR_R_BIN=/opt/R/$R_VERSION/bin/R local/uvr-install.sh RPostgres httr2 jsonlite + - UVR_R_BIN=/opt/R/$R_VERSION/bin/R local/uvr-install.sh RPostgres httr2 jsonlite data.table - /opt/R/$R_VERSION/bin/R -q -e 'source("local/weekly-missing-binaries-audit.R")' backend_options: docker: diff --git a/.crow/weekly-rebuild-missing.yaml b/.crow/weekly-rebuild-missing.yaml index a93a7bd..e54d107 100644 --- a/.crow/weekly-rebuild-missing.yaml +++ b/.crow/weekly-rebuild-missing.yaml @@ -373,6 +373,8 @@ steps: R_LIBS_USER: /mnt/cache/R-pkgs R_VERSION: ${R_VERSION} CCACHE_DIR: /mnt/cache/ccache + # parallel make within one package build, matched to the step's CPU limit + MAKE_JOBS: 3 PLATFORM: ${OS} ARCH: ${ARCH} NCPUS: 2 @@ -384,12 +386,13 @@ steps: REBUILD_BUDGET_HOURS: 20 commands: - git clone -q https://pat-s:$$REPO_RO_TOKEN@git.devxy.io/devxy/build-cran-binaries.git . + - . local/compiler-cache.sh - mkdir -p /mnt/cache/uvr/cache /mnt/cache/uvr/packages /mnt/cache/R-pkgs /mnt/cache/ccache /mnt/cache/packages - rm -rf /mnt/cache/R-pkgs/00LOCK-* - /opt/R/$R_VERSION/bin/Rscript local/install-bincraft.R - /opt/R/$R_VERSION/bin/R -q -e 'packageVersion("bincraft")' - XVFB=$(command -v xwfb-run 2>/dev/null || command -v xvfb-run); XVFB_ARGS="-a"; if command -v xwfb-run >/dev/null 2>&1; then dnf install -y -q weston 2>/dev/null; XVFB_ARGS="-c weston"; fi - - UVR_R_BIN=/opt/R/$R_VERSION/bin/R local/uvr-install.sh httr2 + - UVR_R_BIN=/opt/R/$R_VERSION/bin/R local/uvr-install.sh RPostgres httr2 - /opt/R/$R_VERSION/bin/R -q -e 'source("local/fetch-rebuild-packages-from-issue.R")' - $XVFB $XVFB_ARGS -n $SPLIT_INDEX -- /opt/R/$R_VERSION/bin/Rscript local/rebuild-missing.R $SPLIT_INTO $SPLIT_INDEX $REBUILD_BUDGET_HOURS 2>&1 backend_options: diff --git a/.crow/weekly-rebuild-reindex.yaml b/.crow/weekly-rebuild-reindex.yaml index 34bad32..1800deb 100644 --- a/.crow/weekly-rebuild-reindex.yaml +++ b/.crow/weekly-rebuild-reindex.yaml @@ -4,8 +4,8 @@ # objects in place, so the slot's index still advertises the old MD5 and, for # anything that had been served from source, no Built stamp. Re-indexing from # inside a shard would mean three concurrent `upload_package_index()` calls on -# one prefix: `cranlike::update_PACKAGES()` lists the live bucket, so an early -# lister that uploads last publishes an index missing its siblings' work. +# one prefix: each lists the live bucket, so an early lister that uploads last +# publishes an index missing its siblings' work. # # So it happens exactly once per slot, here, after every shard has finished. # `runs_on: [success, failure]` keeps that true when a shard fails; only an @@ -144,12 +144,11 @@ steps: from_secret: PGPASS REPO_RO_TOKEN: from_secret: REPO_RO_TOKEN - # uvr resolves bincraft's dependencies through the GitHub API, including - # the pat-s/cranlike@s3 fork. Unauthenticated it is rate limited, and the - # failure is not loud: uvr warns, falls back to the registry copy of - # cranlike, and the index write then dies on `unused argument (built = - # built)` because upstream has no such parameter. Intermittent by nature, - # since it depends on the rate-limit window. + # bincraft reads the DESCRIPTION of every new or replaced tarball from + # the CRAN mirror on raw.githubusercontent.com, and authenticates with + # this token when it is set. A slot whose published index cannot be read + # is re-read in full, ~25k requests, which the anonymous rate limit would + # cut short. GITHUB_PAT: from_secret: GITHUB_PAT GIT_USER: pat-s @@ -162,15 +161,19 @@ steps: - mkdir -p /mnt/cache/R-pkgs - rm -rf /mnt/cache/R-pkgs/00LOCK-* - /opt/R/$R_VERSION/bin/Rscript local/install-bincraft.R + - UVR_R_BIN=/opt/R/$R_VERSION/bin/R local/uvr-install.sh RPostgres # The codename is detected from the image's /etc/os-release. - /opt/R/$R_VERSION/bin/R -q -e 'library(bincraft); upload_package_index(s3_endpoint = "https://s3.eu-central-003.backblazeb2.com", s3_region = "eu-central-003", s3_bucket = "devxy-rpkgs-binaries", s3_access_key_id = Sys.getenv("B2_S3_ACCESS_KEY"), s3_secret_access_key = Sys.getenv("B2_S3_SECRET_KEY"))' - | - failed="" + FAILED_MINORS="" for RBIN in /opt/R/[0-9]*/bin/R; do RMINOR=$(basename "$(dirname "$(dirname "$RBIN")")" | cut -d. -f1-2) - /opt/R/$R_VERSION/bin/R -q -e "library(bincraft); upload_package_index(r_minor = '$RMINOR', s3_endpoint = 'https://s3.eu-central-003.backblazeb2.com', s3_region = 'eu-central-003', s3_bucket = 'devxy-rpkgs-binaries', s3_access_key_id = Sys.getenv('B2_S3_ACCESS_KEY'), s3_secret_access_key = Sys.getenv('B2_S3_SECRET_KEY'), metadata_db_host = 'r-binaries.devxy.io', metadata_db_name = 'build_metadata', metadata_db_port = 15432L, metadata_db_user = 'rpkgs', metadata_db_password = Sys.getenv('PGPASS'), metadata_db_sslmode = 'require')" || failed="$failed $RMINOR" + /opt/R/$R_VERSION/bin/R -q -e "library(bincraft); upload_package_index(r_minor = '$RMINOR', s3_endpoint = 'https://s3.eu-central-003.backblazeb2.com', s3_region = 'eu-central-003', s3_bucket = 'devxy-rpkgs-binaries', s3_access_key_id = Sys.getenv('B2_S3_ACCESS_KEY'), s3_secret_access_key = Sys.getenv('B2_S3_SECRET_KEY'), metadata_db_host = 'r-binaries.devxy.io', metadata_db_name = 'build_metadata', metadata_db_port = 15432L, metadata_db_user = 'rpkgs', metadata_db_password = Sys.getenv('PGPASS'), metadata_db_sslmode = 'verify-full')" || FAILED_MINORS="$FAILED_MINORS $RMINOR" done - if [ -n "$failed" ]; then echo "Per-minor reindex failed for:$failed" >&2; exit 1; fi + if [ -n "$FAILED_MINORS" ]; then + echo "Could not republish the per-minor index for:$FAILED_MINORS" >&2 + exit 1 + fi - name: Purge CDN cache image: reg.devxy.io/docker.io/library/alpine:3.24 diff --git a/.gitignore b/.gitignore index f4124a2..c072661 100644 --- a/.gitignore +++ b/.gitignore @@ -102,3 +102,7 @@ docs/ # Superpowers SDD scratch (briefs, reports, ledger) .superpowers/ + +# Internal design docs (plans and specs) are kept out of the repo. +plans/ +specs/ diff --git a/.markdownlint-cli2.yaml b/.markdownlint-cli2.yaml index 032c0ea..66e7cc8 100644 --- a/.markdownlint-cli2.yaml +++ b/.markdownlint-cli2.yaml @@ -2,6 +2,3 @@ ignores: - LICENSE.md - docs/superpowers/** - # Internal design docs (specs/plans) are not user-facing reference material. - - specs/** - - plans/** diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index e0ba9a9..960f7aa 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -14,8 +14,7 @@ exclude: | # here. The exclusions are per-hook, not global, so `validate patch registry` # still runs when a patch changes. repos: - - repo: https://github.com/pre-commit/pre-commit-hooks - rev: v6.0.0 + - repo: builtin hooks: - id: end-of-file-fixer exclude: ^local/patches/.*\.patch$ @@ -24,15 +23,15 @@ repos: args: - --markdown-linebreak-ext=md - repo: https://github.com/DavidAnson/markdownlint-cli2 - rev: v0.23.3 + rev: v0.24.2 hooks: - id: markdownlint-cli2 - repo: https://github.com/rbubley/mirrors-prettier - rev: v3.9.10 + rev: v3.10.1 hooks: - id: prettier - repo: https://github.com/posit-dev/air-pre-commit - rev: 0.12.0 + rev: 0.13.0 hooks: - id: air-format - repo: https://github.com/editorconfig-checker/editorconfig-checker diff --git a/.terraform.lock.hcl b/.terraform.lock.hcl index 49778e1..7095056 100644 --- a/.terraform.lock.hcl +++ b/.terraform.lock.hcl @@ -2,79 +2,79 @@ # Manual edits may be lost in future updates. provider "registry.opentofu.org/hashicorp/http" { - version = "3.6.2" + version = "3.6.3" hashes = [ - "h1:7+Ewr07oLewLfoe5K7Fhy4T5JUU4y7I7iRGTG2hrzy4=", - "h1:8AvJ6H30J1tgxwEoVKxe0B0Ac9XFn1gwOzidrIc+F5o=", - "h1:AllxtaK2m88aY+QitHui6kik/qJmBakrH5o5W7vOKI0=", - "h1:OimQGm1/9sIkdDCuUb8D5sxDorhektHeCQ337DHDfGk=", - "h1:T5zxkFXQ9To+ZysjCZu6gFqE8G1aTGh58Ol20bCk1Fc=", - "h1:TZlCCHsmKXSI3FyzmSgCU+TarKOHgiOft0wiYh85Y+E=", - "h1:VgyzPdGw1U/KU8gKhefKlIhy5kQYdpoZkpNcGiTgvVs=", - "h1:aF+Ztfy9m0Ll6nFOtC90NxN6WIEKAmfmVFhXAtFF8CI=", - "h1:bgViIoXF7kMXA7xgNikFYfv1D15rregO8I8Sq95sLGQ=", - "h1:d7tCAc1wr1seWpGMBx7tpRyGKOCicvozaHvn5X8ZFfM=", - "h1:dRCrpHH2UrkKs/lCqewyA00L0VfOCETVM14NBhyxHaM=", - "h1:eIqaNIixQTlhmr2GU1fAFH+eVYLQIfeGw7Ec+JT/bEo=", - "h1:hrK+Jzpgivi4Jo356K3LYDoeWBV1bMqKPBEqrEHwgpA=", - "h1:mrpIAC+TK3rQQz2jllAF8o2WLMKM14/WoclLgYKc4WA=", - "h1:zwE/uNBapBDpkKa5I24moawVPTanbG+eKumHRm/qINw=", - "zh:0cf6173a426f65a85e8baf4f9a7808dc94d0e02a82b34bc903555fda1eb45d7a", - "zh:2b5fa2b0487e692014fa86745a2a98d43f34f7aaea044acd3e2cee139627c9f1", - "zh:2c9c18893ce0f34e077559632c88237d172ac6646853bf00c84a5a4ec3b6c241", - "zh:316c848e892764aa10a890cf2455f52b592c6051a92ac8451955ca1d47ca1d2f", - "zh:4300e32ecaa967ca0ed56ca48f8dcd10796e70203bf6b8311e71dc43cd0d2aa1", - "zh:4a0a555d85a31bd257399253b502ad03816ccdba7b8fbbbd1e07a313d04d83fb", - "zh:6278773c31c1605c6d780fc982eacb83944e2807c8d8afe11534253b225c5aa3", - "zh:81b73da8dbcedf54604f131e0acd7ebd36cf673276174b4b4f10af0b0f10de11", - "zh:887beaa6ef6521d3f5e2b1193d458575fb91855f36bc66dd26a10988127a39fb", - "zh:91b2260424c5f8e5968d9e6d5bdbbdb60d61ccc4ff001aa0d637427629349c54", - "zh:a5d9d7032d00a6dd844e8d0d374b5ae89e25f9a6635a90e01c5f65e9dd14a93e", - "zh:bcf8b2aca448774e4701f221b74d762847be55dc312022b4aad66da3b389bf5d", - "zh:d62a3ad37ee1fb943ad0590978f4e7ee0ac064d19554c1c05ec98e9d2bb658f7", - "zh:e517b8b81fe88bf49868923f076515b9157771d1d3a80849cc8a73b9a19039a1", - "zh:fa9e0e39ef3e5ed2e6cf5886a0b569415ca3b89b9289fd6d01824ff11e470618", + "h1:4Qcn3THnwpzEH7EJvhnDYw82YM/+Fs4x5rjtaOdHIGg=", + "h1:DWXJhjYQ66frFZ2edx766sUWDPoPTsWcO8VeL1TQmj0=", + "h1:ISETodakZAv3gUBLFHm1+JpKB0vUXzOSmJGlyTvap0A=", + "h1:IWCGxH32+DkaMeH7xd1SCggVeuhKnKOlkKxlRfQnSlc=", + "h1:JrmuoX6FTlDFJDqveHpyO0xCOyWd40BVTAEFGmwbsPI=", + "h1:OqnVFnmMgeKLZ0l32P80o6DdAhd2ug0nJmEcngf6u/U=", + "h1:Se19S4Etttxlm6u/fT0JbvqfsTJjNusWK5pe2Hb1Aso=", + "h1:Sj+741LS543TKq6nf2MXCKzhzEygGgE8ptZKZhWh1N0=", + "h1:T8pVs0L1oME182/NXYte0G65aOxNod8fhKd6Gi7l+04=", + "h1:VYtHHeZQ+jzrqFppK/eAzdj3dMXn4wPYvfWn9STSaKg=", + "h1:Xkog5MBnip5VSOe8gkMAg/s+fBSpuc2yOPtoMY78mVQ=", + "h1:Xoun8WNhPBF/Acv6vvBJcOtPS30rWL8bDbPBXAZkWXA=", + "h1:b8JSDcp7TH6fQYII+ZZSrMlyAyyP8oSA6FPLs+2Hm3g=", + "h1:bK4m/bKTU8PjLG4G3wEdoRjqmFSB222h1ue0Jxdsp1U=", + "h1:sIiP0wiUT/sXTzwxUoAcEnTgxDkWQZUspChLs/SbXI8=", + "zh:0551a71cf3c063c8c4925fb3bc95e9145da6440887e8513040ea49ea1ecf61ed", + "zh:19f379bb524434d05f5c917371c8d993491058cf577a6c66ebbf1b26a9924003", + "zh:2da5794d9f33f5ba8bdc74de7422b3289f1862be7583fcd86328d28a35a533d9", + "zh:3e3d463e11d3a0d1eb54d4a4936d5de42395271e00a0aa884b27f00ca98d246d", + "zh:476a140b360acb9ca589b7de6a5a8a379aabab4e034fd2dbf4be033c21749907", + "zh:47f46c2a71075a414049f9c3382dad1b6a0fa3b02a67ed8b7db1e7e24dac1960", + "zh:5845ccfc22524f8b2b76a8b790569183e70962b05f8767738edcd4a1b86c0c23", + "zh:80b341609f5f962f709bae424860334e69e1fe6d12b9027f9c0355a1da63eb02", + "zh:94cd85fd04a0b36a8e622efc63e08c34a6ba135713dd9c7a60059af55aea6876", + "zh:abee98f71480a9485f85dee138d9c120d0a0bccb77c700e03fe9585ad0538673", + "zh:ba330dc3fd22cb646d225b0e5e1263a1aa5493c7e880f97b22788e3702a93acd", + "zh:c3534ef0998dba2a3b79d4c993197ed86f1482dd451479ae8244d92cf7b3b3a9", + "zh:d2a2d76caa8256430cae2238f91ca4d5cdb8aa50cccd0c057c97c229ae8508d5", + "zh:ec3f1132210103816061bcff2043e852d9dc2add1857af4620f49059771e1646", + "zh:f3294fc74d18ada739f968b4e0b5ec65c0acb6a821c777909c6c54e3c54cf771", ] } provider "registry.terraform.io/bunnyway/bunnynet" { - version = "0.19.2" - constraints = "~> 0.19" + version = "0.20.3" + constraints = "~> 0.20" hashes = [ - "h1:1RGbDsnU2r+Fp7AVTZkPE29CIY6z/qfSzb47YnCBCTs=", - "h1:6tyZaU4EVdCFkSaCF2Rn14s29CjPfiIZzn8rEleluH4=", - "h1:9Hb0HF83T+cIl/jFyFBxVjTfSgUZHWlxFQpCcct767M=", - "h1:BPwOWd/UY7MNt71Gat+TApEdQtME6LojWBLR3BAGHhw=", - "h1:Bvg7QNBJXsniFMQfxYQvRSuZJQYl9vtrIY5k1wyuIww=", - "h1:C+Q6JJU8K0yoaXv9n1fKKTsRz92uYaPWQ/N+jxZHXNU=", - "h1:GExlP3x5XDYbQvPSXBq8GrWNMbWTxNP005hUnFXzIKY=", - "h1:IyU50/IJkOjGJm1zuEH7Ui0w3//xv/V2H+lWcUWLMbM=", - "h1:QooUpHtR0U8G0E2p5VThFHiHiy1YHGPnefNrNqFC1Oo=", - "h1:RElSrKI43XR3coq4XGfF3AInuAS4cPphqq6NsxIeDU0=", - "h1:cL/YPyl/h1ZOuJAJ49xZUO6mKes62uxWJ8esHi7vUmk=", - "h1:jQYXBctHWU34H0VoIT7OULdNAgngUEsfo3HJMZIYNeE=", - "h1:kZ5NHHgmovqTexzDrksOiAq5tdhQbQBs4SQn+nA23uk=", - "h1:lh4A6QWikA6duGgOT8FmdvAKigOo1CEuCFnbdcqLCQs=", - "h1:mFkPTvdXHXiDCtRmgAlPcOPYKms4IR0/VrgI0S/tUWQ=", - "h1:rjKiZjk7HBNKY5dQPmLI0v7JO4o0zrfXeGB/mMRlnOo=", - "h1:uV5e4h8EfgFvppbg1jUfGhEK42xkluVgddbvSzjOu5o=", - "zh:0f46034291455bd8382716f7c7b8a942fa3a3e652c7241c3b224573d9855c93a", - "zh:10c9f68f4df54353d2c2f3ed2dbe1de970da45418fa5847d933d916ba7c27190", - "zh:31e62093ea11e9c234740d02ab5b83dce15cea6e1887e8eb2090584ec58e4b7b", - "zh:3656a1c51f7bf8a60584c8798040cdd9159e6d6736160e68c4e3149a45d8b80e", - "zh:3706d49aaae6c7be206b500505e52b1b8aa0bb6345806f421d2360d5c38368f6", - "zh:4971a5cfa0550eac793bd4ab3fb2e50cf0afe8769a003ef7454e75ddf3c48a91", - "zh:5d172a3f57677a37e5bd924ac1ca642125b89616fb1424146b733c73566f6138", - "zh:61ab3d637a93032556e72aa602b663acfb4e957ea394877e2ea6c16da70e3a30", - "zh:6a2e646c90795a054d389c6be313da284d1347607f0a542799befde5716b01b3", - "zh:74c87f30d7b19210de708d669192228da2a29966be5c42bb8d9f4393c427902b", - "zh:8846d868864c384004caef674a9338115080ab9ef5c0b94242c6674f83b0b2cc", + "h1:09RqUsHrSx1IZ7GZsjn5J7vxqG5jv9aTJe9qR7amJJs=", + "h1:0HE3kahGf8gl1wWze3+wA8+oMkRnIQwdVeDmwm6LtZc=", + "h1:1QQgmqEu46pDn0KR27VSQ0Bi2E87CjcNseP4JgQn8C8=", + "h1:3oouE6cghziTZJ+l+udbXi03wNjH/aYKZETkQ3z9+hU=", + "h1:4ABuqzs2BlJu1bNHkHvd0b4NY1PmWYeqtd+32fR3Wmk=", + "h1:5u3+Nk4DpAIuW0mDbItelbiZL5KWKcLLdh/szuXJfEM=", + "h1:DC9++/g5hv8W9M3YvWn27PC/NBXbfKjtI7XbfNy4Y+Q=", + "h1:DH3JLDv9oGtUxQi3eCLzUm9OILS4oBO+/T2iSu7fFAg=", + "h1:FnFnDiIyzoroOSf6zHyejpN1bvyo9PqwfSfWm0FcThs=", + "h1:Lb95sYGHhBThec7qXeUPFyVdkM1ViDytYkA3vYtUfYo=", + "h1:RFA0Pm6G8ZbaIrDnDrSHGXGqcfPIJJCXiWQcO83R5CE=", + "h1:dqebzlaxgTHfPfotskbJ72Nl6ft/exl04CwR9f8Zqz8=", + "h1:iHUmCyzRcantigqlJYtTjFUhjt8udTpShton09isK2c=", + "h1:lltTH48SIjhUY1v5TBIxp96xqC8aaiik0HoAJYUHqQM=", + "h1:o09J6fw7vV/M7J/DYfVHsNUZnk8c3aYMU3p+nnpMWj8=", + "h1:ofBGqkMXdAtVMpmGCXoKQj6hlwsYYk1L7mvuESSxLHk=", + "h1:t/YBNaqpoJuMz7b+TjRdzNuwWbnGZvByAz7wUklX22M=", + "zh:256f3fbfbebb6383ce720001d024bf267c299a48c559d96c20f8f95262030267", + "zh:28dfae8191b836bad53fab92305298d6ce111c825ab6d857794e95c447c09d83", + "zh:315a6a933184bf7aed0e45bec3e820b7196bdc643bed9194a760b74ffb12fd62", + "zh:427c563c207efc9b8805b90fcd79b200510ab8d2e5216a3ece6c44806027dfd5", + "zh:4a71a7f161416d4c3270724c62d04f75c24d0c009b06b1d3521a0df755395d60", + "zh:4edb43070df5112a58baa85bac209465f479713bd722800723bdbf7686f518d6", + "zh:533cdd87680b68c56025a9c60ca0cfc582c285136292be2dd79046126e76bc0d", + "zh:59c1a09dcd148f053ce26ed34788ca05b7307c2e8e8e1f78e6d621b93533f1bf", + "zh:60e1bbe4f679bf5c7565ebea38c4cbca84b578e1458d3ba92e75b31433647516", + "zh:7621c7380f91d44727d53c238bc78c66a04cd2f8e302ae173e2f48d7558fdec6", + "zh:876a61ff9b46f5092531b6808903002285952683e05625d6180acbc8f5d642b0", + "zh:8840722da44ebbf4286b1f2da6263d1f550cad2f7445d4136c98758f7adcfe79", "zh:890df766e9b839623b1f0437355032a3c006226a6c200cd911e15ee1a9014e9f", - "zh:8bbfa009c19853d1208423b720d9a0e72e0df1ae2ca261a8dfb3c597d447919c", - "zh:9c78b14820ef626e896dfb815bc7d2366e3476ef1ec7c148d433c424989c961d", - "zh:cb5d42561f86cf20506d0f164d08f2dd93147d58b6359f973033117d74f3d559", - "zh:d4b68f306ff8e23a5748f70871e0a7ceaff7d2ae0f4df279db0fe519de868b8b", - "zh:e13d71751b65aa64b7d5460ab0626a61f33008b728178547534499c66ff29ae8", - "zh:f885b9fb40d4f8da8401b2a62525951e3b8e95b96839b3f32d42b4645df7718d", + "zh:b2fd5fb60183750d6403a2411eb7a36f3a9a3fad39e9ad4c65c8536f58d54614", + "zh:b69bdd246040e3792a87acbf963d5c3f6bb5b5513dd49b5df3d7ba629450cd79", + "zh:cbbd0b28082eae5585860a4cac1d19b42c9fe692caa167bec4ec0571bcf4634c", + "zh:e08440881ef438969bab652af115e471e5645ed25b4c7e983ec6ba9cf6933505", + "zh:ff7540cebcb08f8a211890dad58777d54a74649a9231b25d10751208371f2826", ] } diff --git a/README.md b/README.md index 99d1143..df69048 100644 --- a/README.md +++ b/README.md @@ -73,8 +73,6 @@ Patching uses a two-tier approach: The system is implemented in `bincraft`: when a package needs patching, `bincraft` pre-builds it with the patch and serves the patched binary to `pak`, ensuring transitive dependents receive the fixed package. This way, the fix cascades to all packages that depend on it. -For the design rationale and architecture, see `specs/2026-06-30-package-patching-design.md`. - ## Build Environment Binaries are built on a mixed-architecture Kubernetes cluster using CI. @@ -103,11 +101,13 @@ Preview the selection without building or accessing storage or build metadata: ### Creating/Updating the PACKAGES Index Files -Currently, the {cranlike} and {desc} packages only work with files on a local file system. +Tools such as `tools::write_PACKAGES()` and {cranlike} only index files on a local file system. This is infeasible if the goal is to store binaries in S3. Storing binaries permanently on a disk-based file system would incur significantly higher costs, especially when operating in the cloud. -Hence, [{cranlike}](https://devxy.gitlab.io/r-package-binaries/bincraft/) and [{desc}](https://github.com/pat-s/desc/tree/description-from-remote) were forked to handle files in S3 (through {s3fs}). +Hence, `bincraft::upload_package_index()` builds the index from a listing of the slot in S3 (through {s3fs}). +Each record is read from the package's CRAN `DESCRIPTION` on the GitHub mirror of CRAN, falling back to the `DESCRIPTION` inside the tarball for a package the mirror does not have. +The previously published `PACKAGES.rds` serves as a cache, so only new or replaced tarballs are read. ### Resources @@ -139,6 +139,7 @@ For Alpine, no rules existed until recently, establishing a foundation for semi- The build metadata is stored in a PostgreSQL database. The database has a public endpoint at `r-binaries.devxy.io` and port `15432`. +It serves a Let's Encrypt certificate for that name, so connect with `sslmode=verify-full`; from R, pass `sslrootcert = bincraft::postgres_sslrootcert("verify-full")` as well, since libpq does not read the system trust store by default. It contains one table named `single_builds`, which holds the build metadata for each package: | package_name | tag | platform | error_occurred | build_timestamp | build_duration | error | size | removed | diff --git a/cdn.tf b/cdn.tf index df30273..46b3e70 100644 --- a/cdn.tf +++ b/cdn.tf @@ -164,6 +164,23 @@ resource "bunnynet_pullzone" "cran_rpkgs_com" { add_canonical_header = true + # Access logs feed the public download statistics (`stats/`, published to + # `/stats/downloads.json`). Both settings were already on; declaring them + # keeps an apply from quietly switching off the only source of that data. + # Bunny keeps the logs for three days, which is why `download-stats` runs on + # a schedule. IPs stay anonymized to /24 (/64 for IPv6) at the edge. + log_enabled = true + log_anonymized = true + log_anonymized_style = "OneDigit" + + # The rpkgs.com website reads `/stats/downloads.json` from the browser, so + # JSON needs the CORS header too. The rest is the zone's existing list. + cors_enabled = true + cors_extensions = [ + "css", "eot", "gif", "jpeg", "jpg", "js", "json", "mp3", "mp4", "mpeg", "png", "svg", "ttf", "webm", "webp", + "woff", "woff2", + ] + cache_stale = ["offline", "updating"] block_ips = var.cdn_block_ips diff --git a/local/backfill-abi-classification.R b/local/backfill-abi-classification.R index 0f48392..c964a86 100644 --- a/local/backfill-abi-classification.R +++ b/local/backfill-abi-classification.R @@ -47,7 +47,7 @@ db <- list( metadata_db_port = 15432L, metadata_db_user = "rpkgs", metadata_db_password = Sys.getenv("PGPASS"), - metadata_db_sslmode = "require" + metadata_db_sslmode = "verify-full" ) read_index <- function(index_url) { diff --git a/local/build-all.R b/local/build-all.R index 349092f..04d382f 100644 --- a/local/build-all.R +++ b/local/build-all.R @@ -1,6 +1,7 @@ sink(stdout(), type = "message") options(crayon.enabled = TRUE, future.globals.onReference = NULL) source(file.path("local", "r-minor-helpers.R")) +source(file.path("local", "rebuild-missing-helpers.R")) args <- commandArgs(trailingOnly = TRUE) parsed <- parse_build_args(args) @@ -104,9 +105,10 @@ sprintf( sensitive_only ) -# Split into chunks for this worker -chunks <- split(pkgs, cut(seq_len(nrow(pkgs)), split_into, labels = FALSE)) -chunk <- chunks[[split_index]] +# Interleave rather than cut the alphabetical list into contiguous blocks: build +# cost clusters by name (Rcpp*, rstan*), and contiguous quarters of the last +# full rebuild ran anywhere from 0.1h to 28h. +chunk <- pkgs[shard_slice(seq_len(nrow(pkgs)), split_into, split_index), ] sprintf("# of package versions for this job: %s", nrow(chunk)) # Exclude known problematic packages (single source of truth) @@ -126,23 +128,7 @@ chunk <- chunk[!chunk$Package %in% exclude, ] # environment, so Sys.getenv() would return "" and this pre-filter would query # platform "-" and skip nothing. codename <- bincraft::set_codename(NULL) -platform <- switch( - codename, - jammy = "ubuntu-2204", - noble = "ubuntu-2404", - resolute = "ubuntu-2604", - rhel10 = "redhat-10", - rhel9 = "redhat-9", - rhel8 = "redhat-8", - alpine320 = "alpine-320", - alpine321 = "alpine-321", - alpine322 = "alpine-322", - alpine323 = "alpine-323", - alpine324 = "alpine-324", - alpine325 = "alpine-325", - alpine326 = "alpine-326", - NA_character_ -) +platform <- codename_platform(codename) local_machine <- Sys.info()[["machine"]] arch <- if (grepl("arm64|aarch64", local_machine)) "arm64" else "amd64" con <- DBI::dbConnect( @@ -152,7 +138,8 @@ con <- DBI::dbConnect( port = 15432, user = "rpkgs", password = Sys.getenv("PGPASS"), - sslmode = "require" + sslmode = "verify-full", + sslrootcert = bincraft::postgres_sslrootcert("verify-full") ) # Scope the skip to the R minor this pass is running under. `single_builds` # records `r_version` per attempt, but querying without it made a non-primary @@ -231,35 +218,41 @@ sprintf( length(s3_cache_paths) ) +deadline <- build_deadline() n <- nrow(chunk) -mapply( - function(pkg, ver, sens, i) { - cat(sprintf("[%d/%d] %s_%s (r_minor_sensitive=%s)\n", i, n, pkg, ver, sens)) - bincraft::build_binary_package( - pkg, - tag = ver, - is_r_minor_sensitive = isTRUE(sens), - s3_endpoint = "https://s3.eu-central-003.backblazeb2.com", - s3_region = "eu-central-003", - s3_bucket = "devxy-rpkgs-binaries", - s3_access_key_id = Sys.getenv("B2_S3_ACCESS_KEY"), - s3_secret_access_key = Sys.getenv("B2_S3_SECRET_KEY"), - s3_package_cache = s3_cache, - metadata_db_host = "r-binaries.devxy.io", - metadata_db_name = "build_metadata", - metadata_db_table = "single_builds", - metadata_db_user = "rpkgs", - metadata_db_password = Sys.getenv("PGPASS"), - metadata_db_sslmode = "require", - metadata_db_port = 15432, - archive = TRUE, - patches = "local/patches", - upload = TRUE, - store_build_metadata = TRUE - ) - }, - chunk$Package, - chunk$Version, - chunk$r_minor_sensitive, - seq_len(n) -) +for (i in seq_len(n)) { + if (deadline_reached(deadline)) { + cat(sprintf( + "Build deadline reached after %d/%d package versions; stopping cleanly. The next run resumes from the metadata DB.\n", + i - 1L, + n + )) + break + } + pkg <- chunk$Package[i] + ver <- chunk$Version[i] + sens <- chunk$r_minor_sensitive[i] + cat(sprintf("[%d/%d] %s_%s (r_minor_sensitive=%s)\n", i, n, pkg, ver, sens)) + bincraft::build_binary_package( + pkg, + tag = ver, + is_r_minor_sensitive = isTRUE(sens), + s3_endpoint = "https://s3.eu-central-003.backblazeb2.com", + s3_region = "eu-central-003", + s3_bucket = "devxy-rpkgs-binaries", + s3_access_key_id = Sys.getenv("B2_S3_ACCESS_KEY"), + s3_secret_access_key = Sys.getenv("B2_S3_SECRET_KEY"), + s3_package_cache = s3_cache, + metadata_db_host = "r-binaries.devxy.io", + metadata_db_name = "build_metadata", + metadata_db_table = "single_builds", + metadata_db_user = "rpkgs", + metadata_db_password = Sys.getenv("PGPASS"), + metadata_db_sslmode = "verify-full", + metadata_db_port = 15432, + archive = TRUE, + patches = "local/patches", + upload = TRUE, + store_build_metadata = TRUE + ) +} diff --git a/local/build-newer-r.R b/local/build-newer-r.R index 3d7794f..05ffba0 100644 --- a/local/build-newer-r.R +++ b/local/build-newer-r.R @@ -1,5 +1,6 @@ source(file.path("local", "newer-r-helpers.R")) source(file.path("local", "rebuild-missing-helpers.R")) +source(file.path("local", "r-minor-helpers.R")) args <- commandArgs(trailingOnly = TRUE) pos <- args[!startsWith(args, "--")] @@ -27,8 +28,106 @@ message(sprintf( if ("--dry-run" %in% args) { print(pkgs, row.names = FALSE) } else { + # Most candidates were built on an earlier run. Asking bincraft about each one + # costs a DB connection plus several S3 requests, about 4s a package, so the + # loop spent well over an hour per run re-confirming existing binaries. + # Decide those in bulk first; bincraft still checks whatever is left. + local_machine <- Sys.info()[["machine"]] + arch <- if (grepl("arm64|aarch64", local_machine)) "arm64" else "amd64" + slot_dir <- sprintf( + "devxy-rpkgs-binaries/%s/%s/latest/src/contrib", + arch, + bincraft::set_codename(NULL) + ) + s3fs::s3_file_system( + aws_access_key_id = Sys.getenv("B2_S3_ACCESS_KEY"), + aws_secret_access_key = Sys.getenv("B2_S3_SECRET_KEY"), + endpoint = "https://s3.eu-central-003.backblazeb2.com", + region_name = "eu-central-003", + refresh = TRUE + ) + # NULL when the listing fails, so that route falls back to bincraft. + list_etags <- function(dir) { + info <- tryCatch(s3fs::s3_dir_info(dir), error = function(e) { + message(sprintf("Could not list %s: %s", dir, conditionMessage(e))) + NULL + }) + if (is.null(info)) { + return(NULL) + } + stats::setNames( + gsub('^"|"$', "", as.character(info$etag)), + basename(as.character(info$uri)) + ) + } + # The verdicts bincraft::classify_r_minor_sensitive() would read from its + # cache, fetched in one query rather than one connection per package. + read_verdicts <- function(packages) { + tryCatch( + { + con <- DBI::dbConnect( + RPostgres::Postgres(), + dbname = "build_metadata", + host = "r-binaries.devxy.io", + port = 15432, + user = "rpkgs", + password = Sys.getenv("PGPASS"), + sslmode = "verify-full", + sslrootcert = bincraft::postgres_sslrootcert("verify-full") + ) + on.exit(DBI::dbDisconnect(con), add = TRUE) + rows <- DBI::dbGetQuery( + con, + paste( + "SELECT package, version, r_minor_sensitive FROM abi_classification", + "WHERE classifier_sig = $1 AND package = ANY($2::text[])" + ), + params = list( + bincraft:::abi_classifier_signature(), + paste0("{", paste(packages, collapse = ","), "}") + ) + ) + stats::setNames( + as.logical(rows$r_minor_sensitive), + paste(rows$package, rows$version, sep = "_") + ) + }, + error = function(e) { + message(sprintf("Could not read ABI verdicts: %s", conditionMessage(e))) + logical() + } + ) + } + sensitive <- read_verdicts(pkgs$Package) + before <- nrow(pkgs) + pkgs <- newer_r_outstanding( + pkgs, + sensitive, + list_etags(slot_dir), + list_etags(file.path( + slot_dir, + sub("^(\\d+\\.\\d+).*", "\\1", target_r) + )), + bincraft:::cran_source_md5() + ) + message(sprintf( + "%d of %d already carry a binary at their routed path; %d left for bincraft", + before - nrow(pkgs), + before, + nrow(pkgs) + )) + options(crayon.enabled = TRUE, Ncpus = 4, future.globals.onReference = NULL) + deadline <- build_deadline() for (i in seq_len(nrow(pkgs))) { + if (deadline_reached(deadline)) { + message(sprintf( + "Build deadline reached after %d/%d package(s); stopping cleanly.", + i - 1L, + nrow(pkgs) + )) + break + } message(sprintf( "[%d/%d] %s %s", i, @@ -48,7 +147,7 @@ if ("--dry-run" %in% args) { metadata_db_name = "build_metadata", metadata_db_user = "rpkgs", metadata_db_password = Sys.getenv("PGPASS"), - metadata_db_sslmode = "require", + metadata_db_sslmode = "verify-full", metadata_db_port = 15432 ), s3_endpoint = "https://s3.eu-central-003.backblazeb2.com", @@ -61,7 +160,7 @@ if ("--dry-run" %in% args) { metadata_db_table = "single_builds", metadata_db_user = "rpkgs", metadata_db_password = Sys.getenv("PGPASS"), - metadata_db_sslmode = "require", + metadata_db_sslmode = "verify-full", metadata_db_port = 15432, archive = TRUE, patches = "local/patches", diff --git a/local/build-one.R b/local/build-one.R index 47dafd2..25aae6e 100644 --- a/local/build-one.R +++ b/local/build-one.R @@ -115,7 +115,7 @@ for (ver in versions) { metadata_db_table = "single_builds", metadata_db_user = "rpkgs", metadata_db_password = Sys.getenv("PGPASS"), - metadata_db_sslmode = "require", + metadata_db_sslmode = "verify-full", metadata_db_port = 15432 ) if (sensitive) touched_minor <- TRUE else touched_generic <- TRUE @@ -159,12 +159,6 @@ if (touched_minor) { s3_region = s3$s3_region, s3_bucket = s3$s3_bucket, s3_access_key_id = s3$s3_access_key_id, - s3_secret_access_key = s3$s3_secret_access_key, - metadata_db_host = "r-binaries.devxy.io", - metadata_db_name = "build_metadata", - metadata_db_port = 15432L, - metadata_db_user = "rpkgs", - metadata_db_password = Sys.getenv("PGPASS"), - metadata_db_sslmode = "require" + s3_secret_access_key = s3$s3_secret_access_key ) } diff --git a/local/compiler-cache.sh b/local/compiler-cache.sh new file mode 100644 index 0000000..8e179b2 --- /dev/null +++ b/local/compiler-cache.sh @@ -0,0 +1,32 @@ +# Sourced (`. local/compiler-cache.sh`) by the build pipelines before any +# package is compiled. POSIX sh: the Alpine images run steps under busybox. +# +# Routes gcc/g++ through ccache. The images name their compilers by bare +# `gcc`/`g++` in both R's Makeconf and /root/.R/Makevars, so prepending ccache's +# masquerade dir catches every compile, CMake-driven vendored builds included. +# Wrapping CC in a Makevars instead would need R_MAKEVARS_USER, which replaces +# the images' /root/.R/Makevars (CXX_STD, the musl defines) rather than adding +# to it. The dir differs per distro: Alpine, RHEL, Debian/Ubuntu. +for dir in /usr/lib/ccache/bin /usr/lib64/ccache /usr/lib/ccache; do + if [ -x "$dir/gcc" ]; then + PATH="$dir:$PATH" + break + fi +done +export PATH + +# R compiles every package in a fresh /tmp/RtmpXXXXXX/ dir. R's default +# flags include -g, which makes ccache hash the working directory, and the line +# markers carry the absolute source path, so without these two settings no +# compile ever hits the cache across runs. +export CCACHE_BASEDIR="${CCACHE_BASEDIR:-/tmp}" +export CCACHE_NOHASHDIR=1 +# The images are rebuilt regularly, which changes the compiler's mtime but not +# its content. ccache's default mtime check would discard the cache each time. +export CCACHE_COMPILERCHECK=content + +# Parallel make inside a single package build. Match it to the step's CPU limit +# via MAKE_JOBS; an explicit MAKEFLAGS wins. +export MAKEFLAGS="${MAKEFLAGS:--j${MAKE_JOBS:-2}}" + +echo "compiler-cache: $(command -v gcc), CCACHE_DIR=${CCACHE_DIR:-unset}, MAKEFLAGS=$MAKEFLAGS" diff --git a/local/failing-builds-report.R b/local/failing-builds-report.R index ce7b683..b9df756 100644 --- a/local/failing-builds-report.R +++ b/local/failing-builds-report.R @@ -69,7 +69,8 @@ con <- DBI::dbConnect( port = 15432, user = "rpkgs", password = Sys.getenv("PGPASS"), - sslmode = "require" + sslmode = "verify-full", + sslrootcert = bincraft::postgres_sslrootcert("verify-full") ) on.exit(DBI::dbDisconnect(con), add = TRUE) diff --git a/local/label-removed-cran-packages.R b/local/label-removed-cran-packages.R index 12dff9d..a6ba36a 100644 --- a/local/label-removed-cran-packages.R +++ b/local/label-removed-cran-packages.R @@ -13,7 +13,8 @@ con <- DBI::dbConnect( port = 15432, user = "r_binaries", password = Sys.getenv("PGPASS"), - sslmode = "require" + sslmode = "verify-full", + sslrootcert = bincraft::postgres_sslrootcert("verify-full") ) pkgs_db <- query_metadata_table() |> diff --git a/local/list-unsafe-flat-objects.R b/local/list-unsafe-flat-objects.R index 60649a2..fca0a9d 100644 --- a/local/list-unsafe-flat-objects.R +++ b/local/list-unsafe-flat-objects.R @@ -30,7 +30,8 @@ con <- DBI::dbConnect( port = 15432L, user = "rpkgs", password = Sys.getenv("PGPASS"), - sslmode = "require" + sslmode = "verify-full", + sslrootcert = bincraft::postgres_sslrootcert("verify-full") ) on.exit(try(DBI::dbDisconnect(con), silent = TRUE), add = TRUE) diff --git a/local/missing-cran-packages-db.R b/local/missing-cran-packages-db.R index 882bca4..0c73e99 100644 --- a/local/missing-cran-packages-db.R +++ b/local/missing-cran-packages-db.R @@ -10,7 +10,8 @@ con <- DBI::dbConnect( port = 15432, user = "r_binaries", password = Sys.getenv("PGPASS"), - sslmode = "require" + sslmode = "verify-full", + sslrootcert = bincraft::postgres_sslrootcert("verify-full") ) `%nin%` <- Negate(`%in%`) @@ -45,12 +46,12 @@ data <- dbGetQuery( "SELECT name FROM single_builds WHERE platform = $1 AND arch = $2 and removed = FALSE;", params = list(platform, arch) ) -removed_pkgs = get_removed_cran_packages(lubridate::interval( +removed_pkgs <- get_removed_cran_packages(lubridate::interval( lubridate::today(), lubridate::today() - 2 ))$name pkgs_db <- unique(data$name) -pkgs_db = setdiff(pkgs_db, removed_pkgs) +pkgs_db <- setdiff(pkgs_db, removed_pkgs) pkgs <- setdiff(cran_pkgs, pkgs_db) formatted_pkgs <- gsub( diff --git a/local/newer-r-helpers.R b/local/newer-r-helpers.R index 4b83204..b4ac689 100644 --- a/local/newer-r-helpers.R +++ b/local/newer-r-helpers.R @@ -73,3 +73,42 @@ newer_r_candidates <- function(db, primary_r, target_r) { result <- db[selected, c("Package", "Version"), drop = FALSE] result[order(result$Package), , drop = FALSE] } + +# Candidates that still need a build, decided from one bucket listing per slot +# instead of a metadata-DB connection and several S3 requests per package. +# +# Mirrors bincraft's own skip decision: a package is done when a binary sits at +# the path its cached ABI verdict routes it to, `//` when +# r-minor-sensitive and the flat `/` otherwise. A CRAN source sitting at +# that key is a fallback, not a binary (see outstanding_packages()). Whatever +# this cannot decide, no cached verdict or a slot that could not be listed, +# stays in the list, so bincraft makes that call exactly as before. +# +# `sensitive` cached verdicts for the current classifier, named `_` +# `flat_etags` listing of the flat slot as for outstanding_packages(), or +# NULL when it could not be listed +# `minor_etags` the same for this interpreter's per-minor slot +newer_r_outstanding <- function( + pkgs, + sensitive, + flat_etags, + minor_etags, + cran_md5 +) { + verdict <- unname(sensitive[paste(pkgs$Package, pkgs$Version, sep = "_")]) + version <- stats::setNames(pkgs$Version, pkgs$Package) + keep <- rep(TRUE, nrow(pkgs)) + routes <- list( + list(sensitive = FALSE, etags = flat_etags), + list(sensitive = TRUE, etags = minor_etags) + ) + for (route in routes) { + idx <- which(verdict %in% route$sensitive) + if (length(idx) == 0L || is.null(route$etags)) { + next + } + keep[idx] <- pkgs$Package[idx] %in% + outstanding_packages(pkgs$Package[idx], route$etags, version, cran_md5) + } + pkgs[keep, , drop = FALSE] +} diff --git a/local/packages-to-build.R b/local/packages-to-build.R index 31b667f..32f846e 100644 --- a/local/packages-to-build.R +++ b/local/packages-to-build.R @@ -5,10 +5,8 @@ options(error = function() { }) library(bincraft, quietly = TRUE) -suppressPackageStartupMessages(library(dplyr)) library(DBI, quietly = TRUE) library(purrr, quietly = TRUE) -library(progressr, quietly = TRUE) suppressPackageStartupMessages(library(data.table)) # Sys.setenv("OS" = "alpine") @@ -32,7 +30,8 @@ con <- DBI::dbConnect( port = 15432, user = "rpkgs", password = Sys.getenv("PGPASS"), - sslmode = "require" + sslmode = "verify-full", + sslrootcert = bincraft::postgres_sslrootcert("verify-full") ) cran_archive <- tools::CRAN_archive_db() diff --git a/local/packages-without-any-binary.R b/local/packages-without-any-binary.R index f42196f..870dc6e 100644 --- a/local/packages-without-any-binary.R +++ b/local/packages-without-any-binary.R @@ -8,7 +8,8 @@ con <- DBI::dbConnect( port = 15432, user = "r_binaries", password = Sys.getenv("PGPASS"), - sslmode = "require" + sslmode = "verify-full", + sslrootcert = bincraft::postgres_sslrootcert("verify-full") ) cran_pkgs <- tools::CRAN_package_db() |> @@ -27,7 +28,7 @@ pkgs <- data |> distinct(name) |> pull(name) -pkgs = setdiff(cran_pkgs, pkgs) +pkgs <- setdiff(cran_pkgs, pkgs) # Format, remove line breaks, and print as a single line formatted_pkgs <- gsub( '"', diff --git a/local/proposal-tracking.R b/local/proposal-tracking.R index f8e1284..82d0beb 100644 --- a/local/proposal-tracking.R +++ b/local/proposal-tracking.R @@ -53,7 +53,8 @@ con <- DBI::dbConnect( port = 15432, user = "rpkgs", password = Sys.getenv("PGPASS"), - sslmode = "require" + sslmode = "verify-full", + sslrootcert = bincraft::postgres_sslrootcert("verify-full") ) on.exit(DBI::dbDisconnect(con), add = TRUE) diff --git a/local/propose-patches.R b/local/propose-patches.R index 360df9f..52a62b8 100644 --- a/local/propose-patches.R +++ b/local/propose-patches.R @@ -87,7 +87,8 @@ con <- DBI::dbConnect( port = 15432, user = "rpkgs", password = Sys.getenv("PGPASS"), - sslmode = "require" + sslmode = "verify-full", + sslrootcert = bincraft::postgres_sslrootcert("verify-full") ) on.exit(DBI::dbDisconnect(con), add = TRUE) diff --git a/local/prune-stale-flat-binaries.R b/local/prune-stale-flat-binaries.R index aea97b4..a821d3f 100644 --- a/local/prune-stale-flat-binaries.R +++ b/local/prune-stale-flat-binaries.R @@ -86,7 +86,8 @@ if (length(explicit) > 0L) { port = 15432L, user = "rpkgs", password = Sys.getenv("PGPASS"), - sslmode = "require" + sslmode = "verify-full", + sslrootcert = bincraft::postgres_sslrootcert("verify-full") ) on.exit(try(DBI::dbDisconnect(con), silent = TRUE), add = TRUE) diff --git a/local/r-minor-helpers.R b/local/r-minor-helpers.R index 65aafdf..b031265 100644 --- a/local/r-minor-helpers.R +++ b/local/r-minor-helpers.R @@ -31,3 +31,33 @@ parse_build_args <- function(args) { ncpus = as.integer(pos[3L]) ) } + +# The `single_builds` platform name for a bincraft codename, e.g. alpine324 -> +# "alpine-324", rhel9 -> "redhat-9". NA for a codename this repo does not build. +codename_platform <- function(codename) { + ubuntu <- c(jammy = "2204", noble = "2404", resolute = "2604") + if (codename %in% names(ubuntu)) { + return(paste0("ubuntu-", ubuntu[[codename]])) + } + if (grepl("^rhel[0-9]+$", codename)) { + return(sub("^rhel", "redhat-", codename)) + } + if (grepl("^alpine[0-9]+$", codename)) { + return(sub("^alpine", "alpine-", codename)) + } + NA_character_ +} + +# The wall-clock deadline a build loop must stop at, from BUILD_DEADLINE (epoch +# seconds). The pipeline sets it once per step, so every pass in the step shares +# one budget and the steps after the builds (archiving, the index upload) still +# get to run before Crow's timeout kills the step. Unset or unparsable means no +# deadline. +build_deadline <- function(value = Sys.getenv("BUILD_DEADLINE", unset = "")) { + deadline <- suppressWarnings(as.numeric(value)) + if (length(deadline) != 1L || is.na(deadline)) Inf else deadline +} + +deadline_reached <- function(deadline, now = as.numeric(Sys.time())) { + now >= deadline +} diff --git a/local/rebuild-missing.R b/local/rebuild-missing.R index 369024e..5d518e0 100644 --- a/local/rebuild-missing.R +++ b/local/rebuild-missing.R @@ -171,7 +171,7 @@ for (i in seq_along(mine)) { metadata_db_table = "single_builds", metadata_db_user = "rpkgs", metadata_db_password = Sys.getenv("PGPASS"), - metadata_db_sslmode = "require", + metadata_db_sslmode = "verify-full", metadata_db_port = 15432, archive = TRUE, upload = TRUE, diff --git a/local/repair-built-stamp.R b/local/repair-built-stamp.R index 8c3bc83..190b145 100644 --- a/local/repair-built-stamp.R +++ b/local/repair-built-stamp.R @@ -9,21 +9,22 @@ ### fail wherever a system `-dev` library is missing. ### ### Rewriting it is not a matter of re-running the normal index update. -### `upload_package_index()` reuses the slot's remote `PACKAGES.db`, and -### cranlike's `update_db()` only reparses files whose md5 changed, so entries -### already in the database keep the stamp they were written with. Dropping -### `PACKAGES.db` to force a full reparse does work, but for an S3 repo cranlike -### reads each package's metadata from the CRAN *source* mirror on GitHub, so a -### 25k-entry slot means 25k requests to raw.githubusercontent.com and a real -### risk of being rate-limited part-way through. +### `upload_package_index()` uses the slot's published `PACKAGES.rds` as its +### cache: a record whose tarball is still listed with the same ETag is reused, +### stamp included, and only new or replaced tarballs are read and stamped. A +### full re-read would mean 25k requests to raw.githubusercontent.com per slot +### and a real risk of being rate-limited part-way through. ### ### Only the `Built` column is wrong, so correct it in place instead: patch the -### column in `PACKAGES.db`, put the database back, and let -### `upload_package_index()` re-emit `PACKAGES*` from it. `update_db()` always -### rewrites the index files even when nothing was reparsed, so no tarball is -### re-read and nothing is fetched from GitHub. +### column in the published `PACKAGES.rds`, put it back, and let +### `upload_package_index()` re-emit `PACKAGES*` from it. No tarball is re-read +### and nothing is fetched from GitHub. ### -### The replacement comes from `bincraft::built_stamp()` under the R running +### bincraft releases before the switch away from cranlike (bincraft#33) read +### the slot's `PACKAGES.db` instead, so with one of those installed the script +### patches that database, as it always did. +### +### The replacement comes from bincraft's `built_stamp()` under the R running ### this script, so run it under the R version the slot should advertise (the ### `R_VERSION` its entry in `.crow/process-updates.yaml` uses). That is what a ### healthy `upload_package_index()` run would have written. @@ -61,8 +62,9 @@ if (is.null(codename) || is.na(codename) || !nzchar(codename)) { } # built_stamp() refuses an unusable platform, so a broken build image fails here -# rather than writing a second bad stamp over the first one. -stamp <- bincraft::built_stamp() +# rather than writing a second bad stamp over the first one. It is internal to +# bincraft, hence `:::`. +stamp <- bincraft:::built_stamp() message(sprintf("Slot: %s/%s", arch, codename)) message(sprintf("New stamp: %s", stamp)) @@ -76,7 +78,12 @@ s3fs::s3_file_system( base_dir <- file.path(bucket, arch, codename, "latest", "src", "contrib") -# A stamp is broken when its platform component is absent or literally "NA". +# A stamp is broken when its platform component is literally "NA" or empty. +broken_stamp <- function(built) { + !is.na(built) & grepl("; NA;|; ;", built) +} +# The same test for PACKAGES.db, which also counts a missing stamp as broken: +# the database never holds a deliberately cleared one. broken_stamp_where <- paste( "Built IS NULL", "OR Built LIKE '%; NA;%'", @@ -91,9 +98,70 @@ r_minors <- sub( list.dirs("/opt/R", recursive = FALSE) ) r_minors <- unique(grep("^[0-9]+\\.[0-9]+$", r_minors, value = TRUE)) -slots <- c(base_dir, file.path(base_dir, r_minors)) +slots <- c( + stats::setNames(base_dir, NA_character_), + stats::setNames(file.path(base_dir, r_minors), r_minors) +) -repair_slot <- function(slot) { +# bincraft imports cranlike for as long as it keeps the index in PACKAGES.db. +uses_packages_db <- "cranlike" %in% loadedNamespaces() + +repair_packages_rds <- function(slot, r_minor) { + rds_remote <- file.path(slot, "PACKAGES.rds") + if (!s3fs::s3_file_exists(rds_remote)) { + message(sprintf(" %s: no PACKAGES.rds, skipping", slot)) + return(invisible(NULL)) + } + + rds_local <- tempfile(fileext = ".rds") + on.exit(unlink(rds_local), add = TRUE) + s3fs::s3_file_download(rds_remote, rds_local, overwrite = TRUE) + records <- readRDS(rds_local) + + # A per-minor index is published as a union with the generic slot, and only + # the rows carrying the slot's own `Path` are reused from it; the generic ones + # are re-read from the generic slot's index, which is repaired on its own. + own <- if (!("Path" %in% colnames(records))) { + rep(TRUE, nrow(records)) + } else if (is.na(r_minor)) { + is.na(records[, "Path"]) + } else { + records[, "Path"] %in% r_minor + } + # A missing stamp is not broken here: bincraft clears it on purpose for a + # package whose build failed and that is served as its CRAN source. + broken <- own & broken_stamp(records[, "Built"]) + + message(sprintf( + " %s: %s entries, %s broken", + slot, + sum(own), + sum(broken) + )) + if (!any(broken)) { + return(invisible(NULL)) + } + if (!apply_changes) { + message(" (dry run, pass --apply to rewrite)") + return(invisible(NULL)) + } + + records[broken, "Built"] <- stamp + saveRDS(records, rds_local, compress = "xz") + s3fs::s3_file_upload( + rds_local, + rds_remote, + overwrite = TRUE, + CacheControl = "no-store" + ) + message(sprintf( + " rewrote %s entries and uploaded PACKAGES.rds", + sum(broken) + )) + invisible(NULL) +} + +repair_packages_db <- function(slot, r_minor) { db_remote <- file.path(slot, "PACKAGES.db") if (!s3fs::s3_file_exists(db_remote)) { message(sprintf(" %s: no PACKAGES.db, skipping", slot)) @@ -134,16 +202,21 @@ repair_slot <- function(slot) { invisible(NULL) } -invisible(lapply(slots, repair_slot)) +repair_slot <- if (uses_packages_db) repair_packages_db else repair_packages_rds +message(sprintf( + "Patching: %s", + if (uses_packages_db) "PACKAGES.db" else "PACKAGES.rds" +)) +invisible(Map(repair_slot, slots, names(slots))) if (!apply_changes) { message("Dry run complete; nothing was changed.") quit(save = "no") } -# Re-emit PACKAGES/PACKAGES.gz/PACKAGES.rds from the corrected database. Nothing -# is reparsed, because no tarball's md5 changed. -message("Re-emitting index files from the corrected database...") +# Re-emit PACKAGES/PACKAGES.gz/PACKAGES.rds from the corrected records. Nothing +# is reparsed, because no tarball changed. +message("Re-emitting index files from the corrected records...") bincraft::upload_package_index( codename = codename, s3_endpoint = endpoint, @@ -152,24 +225,44 @@ bincraft::upload_package_index( s3_access_key_id = Sys.getenv("B2_S3_ACCESS_KEY"), s3_secret_access_key = Sys.getenv("B2_S3_SECRET_KEY") ) +# A per-minor index reads the ABI classifier cache and the flat-object safety +# table from the build database. A failed minor does not stop the others, but +# it does fail the run once they are done. +failed_minors <- character(0L) for (minor in r_minors) { - try( - bincraft::upload_package_index( - codename = codename, - r_minor = minor, - s3_endpoint = endpoint, - s3_region = region, - s3_bucket = bucket, - s3_access_key_id = Sys.getenv("B2_S3_ACCESS_KEY"), - s3_secret_access_key = Sys.getenv("B2_S3_SECRET_KEY"), - metadata_db_host = "r-binaries.devxy.io", - metadata_db_name = "build_metadata", - metadata_db_port = 15432L, - metadata_db_user = "rpkgs", - metadata_db_password = Sys.getenv("PGPASS"), - metadata_db_sslmode = "require" - ), - silent = FALSE + ok <- tryCatch( + { + bincraft::upload_package_index( + codename = codename, + r_minor = minor, + s3_endpoint = endpoint, + s3_region = region, + s3_bucket = bucket, + s3_access_key_id = Sys.getenv("B2_S3_ACCESS_KEY"), + s3_secret_access_key = Sys.getenv("B2_S3_SECRET_KEY"), + metadata_db_host = "r-binaries.devxy.io", + metadata_db_name = "build_metadata", + metadata_db_port = 15432L, + metadata_db_user = "rpkgs", + metadata_db_password = Sys.getenv("PGPASS"), + metadata_db_sslmode = "verify-full" + ) + TRUE + }, + error = function(e) { + message(sprintf("Re-indexing %s failed: %s", minor, conditionMessage(e))) + FALSE + } + ) + if (!ok) { + failed_minors <- c(failed_minors, minor) + } +} +if (length(failed_minors) > 0L) { + stop( + "Could not republish the per-minor index for: ", + paste(failed_minors, collapse = ", "), + call. = FALSE ) } diff --git a/local/seed-helpers.R b/local/seed-helpers.R new file mode 100644 index 0000000..a3d8e57 --- /dev/null +++ b/local/seed-helpers.R @@ -0,0 +1,162 @@ +# Pure helpers for local/seed-pure-r.R, kept separate so local/tests can source +# them without touching S3 or the metadata DB. + +# "alpine" for alpine324, "rhel" for rhel9. Seeding only ever crosses versions +# within one family: a pure-R binary has no compiled code, but install-time R +# code can still capture the build system's paths and tools. +codename_family <- function(codename) { + sub("[0-9]+$", "", codename) +} + +per_minor_key <- function(keys) { + grepl("^[0-9]+\\.[0-9]+/", keys) +} + +# Split `_.tar.gz` file names; NA where a name does not parse. +split_tarball_name <- function(files) { + m <- regmatches(files, regexec("^([A-Za-z0-9.]+)_([^_]+)\\.tar\\.gz$", files)) + part <- function(i) { + vapply(m, function(x) if (length(x) == 3L) x[[i]] else NA_character_, "") + } + list(package = part(2L), version = part(3L)) +} + +# Donor objects worth downloading and verifying for the target slot. +# +# `donor_keys`/`donor_etags` describe the donor slot, keys relative to its +# contrib dir (`curl_5.0.tar.gz`, `Archive/curl/curl_4.3.tar.gz`). +# `target_keys` are the target slot's keys, same form. +# `release_pkgs` packages on CRAN now; the build scope. +# `compiled_pkgs` packages whose current CRAN release needs compilation. Their +# archive versions are skipped as well: a seed only saves time, so guessing +# wrong in this direction costs a build, never a broken binary. +# `cran_md5` CRAN's MD5 per `_`. An ETag equal to it is CRAN's source +# tarball served as a fallback, never a binary worth copying. +# +# Anything already present in the target, under any non-per-minor key, is left +# alone: that is either a build or an earlier seed. +seed_candidates <- function( + donor_keys, + donor_etags, + target_keys, + release_pkgs, + compiled_pkgs, + cran_md5, + exclude = character() +) { + files <- basename(donor_keys) + parsed <- split_tarball_name(files) + md5 <- if (length(cran_md5) == 0L) { + rep(NA_character_, length(files)) + } else { + unname(cran_md5[paste(parsed$package, parsed$version, sep = "_")]) + } + is_source <- !is.na(md5) & !is.na(donor_etags) & donor_etags == md5 + present <- basename(target_keys[!per_minor_key(target_keys)]) + + keep <- !is.na(parsed$package) & + !per_minor_key(donor_keys) & + parsed$package %in% release_pkgs & + !parsed$package %in% compiled_pkgs & + !parsed$package %in% exclude & + !files %in% present & + !is_source + out <- data.frame( + key = donor_keys, + package = parsed$package, + version = parsed$version, + stringsAsFactors = FALSE + )[keep, , drop = FALSE] + # The same version can sit both flat and under Archive/ while the donor waits + # for process_unarchived_pkgs(); copy it once, flat first. + out <- out[order(grepl("^Archive/", out$key)), , drop = FALSE] + out <- out[!duplicated(basename(out$key)), , drop = FALSE] + rownames(out) <- NULL + out +} + +# `Built: R 4.5.3; ; 2026-09-01 10:00:00 UTC; unix` -> R version and platform. +# The platform is empty exactly when the package has no compiled code. +parse_built <- function(built) { + parts <- trimws(strsplit(built, ";", fixed = TRUE)[[1L]]) + list( + r_version = sub("^R\\s+", "", parts[1L]), + platform = if (length(parts) >= 2L) parts[2L] else NA_character_ + ) +} + +# Whether one downloaded donor tarball may be published in the target slot. +# +# Every check has to pass; anything unreadable is a refusal, never a guess, +# because a refusal only means the package gets built the normal way. +# * it is a binary: DESCRIPTION carries a `Built` field (a CRAN source +# fallback does not); +# * it has no compiled code: empty `Built` platform and no libs/ directory; +# * it was built by the target's primary R minor, the one whose binaries the +# flat slot holds; +# * `abi_verdict` (bincraft's upload-time symbol inspection) calls it portable +# across every installed minor, exactly the decision a fresh upload would +# make before writing to the flat slot. +seed_verdict <- function(tarball, package, version, target_minor, abi_verdict) { + reject <- function(reason) { + list(seed = FALSE, reason = reason, unsupported = character()) + } + read_members <- function() { + tryCatch( + utils::untar(tarball, list = TRUE), + error = function(e) NULL, + warning = function(w) NULL + ) + } + members <- read_members() + if (is.null(members)) { + return(reject("unreadable")) + } + desc <- paste0(package, "/DESCRIPTION") + if (!desc %in% members) { + return(reject("unreadable")) + } + if (any(startsWith(members, paste0(package, "/libs/")))) { + return(reject("compiled")) + } + + dir <- tempfile("seed_desc_") + on.exit(unlink(dir, recursive = TRUE, force = TRUE), add = TRUE) + fields <- tryCatch( + { + utils::untar(tarball, files = desc, exdir = dir) + read.dcf( + file.path(dir, desc), + fields = c("Package", "Version", "Built") + ) + }, + error = function(e) NULL, + warning = function(w) NULL + ) + if (is.null(fields) || nrow(fields) != 1L) { + return(reject("unreadable")) + } + if ( + !identical(unname(fields[1L, "Package"]), package) || + !identical(unname(fields[1L, "Version"]), version) + ) { + return(reject("mismatch")) + } + if (is.na(fields[1L, "Built"])) { + return(reject("source")) + } + built <- parse_built(fields[1L, "Built"]) + if (is.na(built$platform) || nzchar(built$platform)) { + return(reject("compiled")) + } + built_minor <- sub("^(\\d+\\.\\d+).*$", "\\1", built$r_version) + if (!identical(built_minor, target_minor)) { + return(reject("r-minor")) + } + + verdict <- abi_verdict(tarball) + if (!isTRUE(verdict$inspected) || !identical(verdict$sensitive, FALSE)) { + return(reject("abi")) + } + list(seed = TRUE, reason = "seeded", unsupported = verdict$unsupported) +} diff --git a/local/seed-pure-r.R b/local/seed-pure-r.R new file mode 100644 index 0000000..4268224 --- /dev/null +++ b/local/seed-pure-r.R @@ -0,0 +1,323 @@ +# Seed a new slot with the pure-R binaries of an existing one. +# +# Usage: Rscript local/seed-pure-r.R DONOR [DONOR...] [--workers=N] [--dry-run] +# e.g. Rscript local/seed-pure-r.R alpine324 alpine323 +# +# Three quarters of the versions a full rebuild targets have no compiled code. +# Building one costs about 20s, nearly all of it dependency installs and +# bookkeeping, and the result is the same R objects every other slot already +# serves: R6 2.6.1 deparses identically on amd64/alpine323, amd64/alpine324 and +# arm64/rhel9. Copying those from a slot of the same OS family and arch saves +# roughly 340 shard-hours per arch and leaves build-all with the compiled rest. +# +# Each candidate is downloaded and checked before it is copied server-side (see +# seed_verdict()): a binary, no compiled code, built by this image's primary R +# minor, and portable across every installed minor by bincraft's own symbol +# inspection. A seeded object then gets the two metadata rows an upload would +# have written: a successful `single_builds` row, so build-all skips it, and an +# `abi_flat_safety` row, without which the per-minor indexes drop it. +# +# Donors are tried in order; the first one with a valid binary wins. Re-running +# is safe: whatever the target already holds is skipped, and the metadata rows +# of earlier copies are re-written idempotently. + +source(file.path("local", "seed-helpers.R")) +source(file.path("local", "r-minor-helpers.R")) + +args <- commandArgs(trailingOnly = TRUE) +donors <- args[!startsWith(args, "--")] +if (length(donors) == 0L) { + stop("Usage: seed-pure-r.R DONOR [DONOR...] [--workers=N] [--dry-run]") +} +dry_run <- "--dry-run" %in% args +workers_arg <- sub("^--workers=", "", grep("^--workers=", args, value = TRUE)) +workers <- if (length(workers_arg)) as.integer(workers_arg[1L]) else 8L +batch_size <- 400L + +local_machine <- Sys.info()[["machine"]] +arch <- if (grepl("arm64|aarch64", local_machine)) "arm64" else "amd64" +target <- bincraft::set_codename(NULL) +platform <- codename_platform(target) +target_minor <- paste( + R.version$major, + strsplit(R.version$minor, ".", fixed = TRUE)[[1L]][1L], + sep = "." +) +for (donor in donors) { + if (identical(donor, target)) { + stop(sprintf("Donor %s is the target slot itself", donor)) + } + if (!identical(codename_family(donor), codename_family(target))) { + stop(sprintf( + "Donor %s is not an %s slot; seeding only copies within one OS family", + donor, + codename_family(target) + )) + } +} +if (is.na(platform)) { + stop(sprintf("No single_builds platform name for codename %s", target)) +} + +# Computed once: bincraft would otherwise run nm over every libR per tarball. +symbol_sets <- bincraft:::installed_r_symbol_sets() +if (is.null(symbol_sets)) { + stop( + "Fewer than two R minors under /opt/R; ABI portability cannot be ", + "verified, so nothing is seeded" + ) +} +message(sprintf( + "Seeding %s/%s (R %s) from %s; ABI checked against R %s", + arch, + target, + target_minor, + paste(donors, collapse = ", "), + paste(names(symbol_sets), collapse = ", ") +)) + +s3fs::s3_file_system( + aws_access_key_id = Sys.getenv("B2_S3_ACCESS_KEY"), + aws_secret_access_key = Sys.getenv("B2_S3_SECRET_KEY"), + endpoint = "https://s3.eu-central-003.backblazeb2.com", + region_name = "eu-central-003", + refresh = TRUE +) +slot_dir <- function(codename) { + sprintf("devxy-rpkgs-binaries/%s/%s/latest/src/contrib", arch, codename) +} +# Keys relative to the contrib dir with unquoted ETags. A listing failure is +# fatal: an empty target listing would mean re-copying everything. +list_slot <- function(codename) { + info <- s3fs::s3_dir_info(slot_dir(codename), recurse = TRUE) + uri <- as.character(info$uri) + is_tarball <- grepl("\\.tar\\.gz$", uri) + data.frame( + key = sub( + sprintf("^(s3://)?%s/", slot_dir(codename)), + "", + uri[is_tarball] + ), + etag = gsub('^"|"$', "", as.character(info$etag[is_tarball])), + stringsAsFactors = FALSE + ) +} + +cran <- tools::CRAN_package_db() +compiled_pkgs <- cran$Package[tolower(cran$NeedsCompilation) %in% "yes"] +cran_md5 <- bincraft:::cran_source_md5() +exclude <- jsonlite::fromJSON("local/excluded-packages.json")[["package"]] + +db_connect <- function() { + DBI::dbConnect( + RPostgres::Postgres(), + dbname = "build_metadata", + host = "r-binaries.devxy.io", + port = 15432, + user = "rpkgs", + password = Sys.getenv("PGPASS"), + sslmode = "verify-full", + sslrootcert = bincraft::postgres_sslrootcert("verify-full") + ) +} + +# The rows an upload would have written for these objects. Idempotent, so a +# re-run can repair rows a failed write left out. Pure-R objects reference no R +# symbols, hence an empty `unsupported`. +record_seeded <- function(packages, versions) { + if (length(packages) == 0L) { + return(invisible(TRUE)) + } + ok <- tryCatch( + { + con <- db_connect() + on.exit(DBI::dbDisconnect(con), add = TRUE) + bincraft:::ensure_flat_safety_table(con, "abi_flat_safety") + DBI::dbWithTransaction(con, { + DBI::dbWriteTable( + con, + "seed_rows", + data.frame( + name = packages, + tag = versions, + stringsAsFactors = FALSE + ), + temporary = TRUE, + overwrite = TRUE + ) + DBI::dbExecute( + con, + paste( + "INSERT INTO single_builds (name, tag, platform, arch,", + "error_occurred, error_text, size, timestamp, duration, removed,", + "r_version)", + "SELECT s.name, s.tag, $1, $2, FALSE, NULL, NULL, now(), NULL,", + "FALSE, $3 FROM seed_rows s WHERE NOT EXISTS (SELECT 1 FROM", + "single_builds b WHERE b.name = s.name AND b.tag = s.tag", + "AND b.platform = $1 AND b.arch = $2 AND b.r_version = $3)" + ), + params = list(platform, arch, target_minor) + ) + DBI::dbExecute( + con, + paste( + "INSERT INTO abi_flat_safety (package, version, codename, arch,", + "safe, unsupported) SELECT s.name, s.tag, $1, $2, TRUE, ''", + "FROM seed_rows s ON CONFLICT (package, version, codename, arch)", + "DO UPDATE SET safe = EXCLUDED.safe,", + "unsupported = EXCLUDED.unsupported, checked_at = now()" + ), + params = list(target, arch) + ) + }) + TRUE + }, + error = function(e) { + message(sprintf( + "WARNING: could not record %d seeded object(s): %s. Re-run to repair.", + length(packages), + conditionMessage(e) + )) + FALSE + } + ) + invisible(ok) +} + +target_listing <- list_slot(target) +message(sprintf("Target %s holds %d tarball(s)", target, nrow(target_listing))) + +future::plan(future::multisession, workers = workers) +tally <- integer() +count <- function(reasons) { + for (r in reasons) { + tally[[r]] <<- (if (is.na(tally[r])) 0L else tally[[r]]) + 1L + } +} + +for (donor in donors) { + donor_listing <- list_slot(donor) + + # Byte-identical copies an earlier run made: make sure their rows exist. + same <- merge(donor_listing, target_listing, by = c("key", "etag")) + same <- same[!per_minor_key(same$key), , drop = FALSE] + parsed <- split_tarball_name(basename(same$key)) + ours <- !is.na(parsed$package) & !parsed$package %in% compiled_pkgs + if (any(ours) && !dry_run) { + record_seeded(parsed$package[ours], parsed$version[ours]) + } + + candidates <- seed_candidates( + donor_listing$key, + donor_listing$etag, + target_listing$key, + cran$Package, + compiled_pkgs, + cran_md5, + exclude + ) + message(sprintf( + "%s: %d tarball(s), %d candidate(s) after the metadata filters", + donor, + nrow(donor_listing), + nrow(candidates) + )) + if (dry_run || nrow(candidates) == 0L) { + next + } + + batches <- split( + seq_len(nrow(candidates)), + ceiling(seq_len(nrow(candidates)) / batch_size) + ) + for (b in seq_along(batches)) { + batch <- candidates[batches[[b]], , drop = FALSE] + dl_dir <- tempfile("seed_") + dir.create(dl_dir) + local_files <- file.path(dl_dir, basename(batch$key)) + downloaded <- tryCatch( + { + s3fs::s3_file_download( + file.path(slot_dir(donor), batch$key), + local_files, + overwrite = TRUE + ) + TRUE + }, + error = function(e) { + message(sprintf( + "Batch %d: download failed (%s); leaving it to build-all", + b, + conditionMessage(e) + )) + FALSE + } + ) + if (!downloaded) { + count(rep("download-failed", nrow(batch))) + unlink(dl_dir, recursive = TRUE) + next + } + + verdicts <- future.apply::future_lapply( + seq_len(nrow(batch)), + function(i) { + seed_verdict( + local_files[i], + batch$package[i], + batch$version[i], + target_minor, + function(t) bincraft:::tarball_abi_verdict(t, symbol_sets) + ) + }, + future.seed = NULL + ) + unlink(dl_dir, recursive = TRUE) + seed <- vapply(verdicts, function(v) isTRUE(v$seed), logical(1L)) + count(vapply(verdicts, function(v) v$reason, "")) + + if (any(seed)) { + copied <- tryCatch( + { + s3fs::s3_file_copy( + paste0("s3://", file.path(slot_dir(donor), batch$key[seed])), + paste0("s3://", file.path(slot_dir(target), batch$key[seed])), + overwrite = TRUE + ) + TRUE + }, + error = function(e) { + message(sprintf( + "Batch %d: copy failed (%s); leaving it to build-all", + b, + conditionMessage(e) + )) + FALSE + } + ) + if (copied) { + record_seeded(batch$package[seed], batch$version[seed]) + target_listing <- rbind( + target_listing, + data.frame(key = batch$key[seed], etag = NA_character_) + ) + } else { + count(rep("copy-failed", sum(seed))) + tally[["seeded"]] <- tally[["seeded"]] - sum(seed) + } + } + message(sprintf( + "%s batch %d/%d: %d of %d seeded", + donor, + b, + length(batches), + sum(seed), + nrow(batch) + )) + } +} + +message( + "Seed summary: ", + paste(sprintf("%s=%d", names(tally), tally), collapse = ", ") +) diff --git a/local/tests/test-newer-r-helpers.R b/local/tests/test-newer-r-helpers.R index c202b7f..ad4ca49 100644 --- a/local/tests/test-newer-r-helpers.R +++ b/local/tests/test-newer-r-helpers.R @@ -1,4 +1,5 @@ source(file.path("..", "newer-r-helpers.R")) +source(file.path("..", "rebuild-missing-helpers.R")) test_that("R constraints respect patch versions, bounds, and missing fields", { expect_true(r_requirement_satisfied(NA_character_, "4.4.3")) @@ -79,3 +80,70 @@ test_that("R-devel Recommended copies cannot block ordinary CRAN dependencies", c("arules", "arulesSequences", "indirect") ) }) + +candidates <- function() { + data.frame( + Package = c( + "flat_done", + "flat_todo", + "minor_done", + "minor_flat_only", + "unclassified", + "fallback" + ), + Version = c("1.0", "1.0", "2.0", "2.0", "3.0", "4.0") + ) +} + +verdicts <- c( + flat_done_1.0 = FALSE, + flat_todo_1.0 = FALSE, + minor_done_2.0 = TRUE, + minor_flat_only_2.0 = TRUE, + fallback_4.0 = FALSE +) + +flat <- c( + flat_done_1.0.tar.gz = "aaa", + minor_flat_only_2.0.tar.gz = "bbb", + unclassified_3.0.tar.gz = "ccc", + fallback_4.0.tar.gz = "c0ffee" +) +minor <- c(minor_done_2.0.tar.gz = "ddd") +md5 <- c(fallback_4.0 = "c0ffee") + +test_that("newer_r_outstanding skips only binaries at the routed path", { + expect_identical( + newer_r_outstanding(candidates(), verdicts, flat, minor, md5)$Package, + # flat_todo: absent; minor_flat_only: sensitive, so a flat copy does not + # count; unclassified: no verdict, left to bincraft; fallback: CRAN source + c("flat_todo", "minor_flat_only", "unclassified", "fallback") + ) +}) + +test_that("newer_r_outstanding keeps a route whose slot could not be listed", { + expect_identical( + newer_r_outstanding(candidates(), verdicts, NULL, minor, md5)$Package, + c("flat_done", "flat_todo", "minor_flat_only", "unclassified", "fallback") + ) + expect_identical( + newer_r_outstanding(candidates(), verdicts, flat, NULL, md5)$Package, + c("flat_todo", "minor_done", "minor_flat_only", "unclassified", "fallback") + ) +}) + +test_that("newer_r_outstanding without verdicts or listings changes nothing", { + expect_identical( + newer_r_outstanding(candidates(), logical(), flat, minor, md5), + candidates() + ) + empty <- stats::setNames(character(), character()) + expect_identical( + nrow(newer_r_outstanding(candidates(), verdicts, empty, empty, md5)), + nrow(candidates()) + ) + expect_identical( + nrow(newer_r_outstanding(candidates()[0L, ], verdicts, flat, minor, md5)), + 0L + ) +}) diff --git a/local/tests/test-r-minor-helpers.R b/local/tests/test-r-minor-helpers.R index ac7b0a6..2beb6c1 100644 --- a/local/tests/test-r-minor-helpers.R +++ b/local/tests/test-r-minor-helpers.R @@ -26,3 +26,20 @@ test_that("compiled but no risky LinkingTo is ambiguous (needs source)", { ) expect_identical(classify_from_metadata("yes", NA, c("Rcpp")), "ambiguous") }) + +test_that("codename_platform names the single_builds platform", { + expect_identical(codename_platform("alpine325"), "alpine-325") + expect_identical(codename_platform("rhel9"), "redhat-9") + expect_identical(codename_platform("noble"), "ubuntu-2404") + expect_identical(codename_platform("resolute"), "ubuntu-2604") + expect_identical(codename_platform("bogus"), NA_character_) +}) + +test_that("build_deadline reads BUILD_DEADLINE and defaults to none", { + expect_identical(build_deadline(""), Inf) + expect_identical(build_deadline("not a number"), Inf) + expect_identical(build_deadline("1700000000"), 1700000000) + expect_false(deadline_reached(Inf)) + expect_true(deadline_reached(100, now = 100)) + expect_false(deadline_reached(100, now = 99)) +}) diff --git a/local/tests/test-seed-helpers.R b/local/tests/test-seed-helpers.R new file mode 100644 index 0000000..87fdd6a --- /dev/null +++ b/local/tests/test-seed-helpers.R @@ -0,0 +1,126 @@ +source(file.path("..", "seed-helpers.R")) + +# A minimal tarball laid out like R CMD INSTALL --build output. +make_tarball <- function( + package = "R6", + version = "2.6.1", + built = "R 4.5.3; ; 2026-09-01 10:00:00 UTC; unix", + libs = FALSE +) { + root <- tempfile("pkg_") + dir.create(file.path(root, package, "R"), recursive = TRUE) + fields <- c(Package = package, Version = version) + if (!is.na(built)) { + fields <- c(fields, Built = built) + } + write.dcf(t(fields), file.path(root, package, "DESCRIPTION")) + writeLines("", file.path(root, package, "R", package)) + if (libs) { + dir.create(file.path(root, package, "libs")) + writeLines("", file.path(root, package, "libs", paste0(package, ".so"))) + } + tarball <- tempfile(fileext = ".tar.gz") + old <- setwd(root) + on.exit(setwd(old), add = TRUE) + utils::tar(tarball, package, compression = "gzip", tar = "internal") + tarball +} + +portable <- function(t) { + list(inspected = TRUE, sensitive = FALSE, unsupported = character()) +} + +test_that("seed_verdict accepts a pure-R binary from the target minor", { + v <- seed_verdict(make_tarball(), "R6", "2.6.1", "4.5", portable) + expect_true(v$seed) + expect_identical(v$reason, "seeded") +}) + +test_that("seed_verdict refuses what a fresh build would not publish flat", { + verdict <- function(tarball, minor = "4.5", abi = portable) { + seed_verdict(tarball, "R6", "2.6.1", minor, abi)$reason + } + expect_identical(verdict(make_tarball(built = NA)), "source") + expect_identical( + verdict(make_tarball( + built = "R 4.5.3; x86_64-pc-linux-musl; 2026-09-01; unix" + )), + "compiled" + ) + expect_identical(verdict(make_tarball(libs = TRUE)), "compiled") + expect_identical(verdict(make_tarball(), minor = "4.6"), "r-minor") + expect_identical(verdict(make_tarball(version = "2.6.0")), "mismatch") + # Unknown is a refusal, as is a verdict that some minor cannot load it. + expect_identical( + verdict(make_tarball(), abi = function(t) list(inspected = FALSE)), + "abi" + ) + expect_identical( + verdict( + make_tarball(), + abi = function(t) list(inspected = TRUE, sensitive = TRUE) + ), + "abi" + ) + junk <- tempfile(fileext = ".tar.gz") + writeLines("not a tarball", junk) + expect_identical(verdict(junk), "unreadable") +}) + +test_that("seed_candidates keeps only uncopied pure-R binaries in scope", { + donor <- c( + "R6_2.6.1.tar.gz", + "Archive/R6/R6_2.5.1.tar.gz", + "curl_7.0.0.tar.gz", + "4.6/R6_2.6.1.tar.gz", + "glue_1.8.0.tar.gz", + "gone_1.0.tar.gz", + "done_1.0.tar.gz", + "skipme_1.0.tar.gz", + "PACKAGES.tar.gz" + ) + etags <- c("a", "b", "c", "d", "c0ffee", "e", "f", "g", "h") + out <- seed_candidates( + donor, + etags, + target_keys = c("done_1.0.tar.gz", "4.6/R6_2.5.1.tar.gz"), + release_pkgs = c("R6", "curl", "glue", "done", "skipme"), + compiled_pkgs = "curl", + cran_md5 = c(glue_1.8.0 = "c0ffee", R6_2.6.1 = "zzz"), + exclude = "skipme" + ) + expect_identical(out$key, c("R6_2.6.1.tar.gz", "Archive/R6/R6_2.5.1.tar.gz")) + expect_identical(out$package, c("R6", "R6")) + expect_identical(out$version, c("2.6.1", "2.5.1")) +}) + +test_that("seed_candidates copies a version listed twice once, flat first", { + out <- seed_candidates( + c("Archive/R6/R6_2.6.1.tar.gz", "R6_2.6.1.tar.gz"), + c("a", "a"), + character(), + "R6", + character(), + character() + ) + expect_identical(out$key, "R6_2.6.1.tar.gz") +}) + +test_that("parse_built separates the R version from the platform", { + b <- parse_built("R 4.5.3; ; 2026-09-01 10:00:00 UTC; unix") + expect_identical(b$r_version, "4.5.3") + expect_identical(b$platform, "") + expect_identical( + parse_built("R 4.4.3; aarch64-alpine-linux-musl; 2026; unix")$platform, + "aarch64-alpine-linux-musl" + ) +}) + +test_that("codename_family groups versions of one distribution", { + expect_identical(codename_family("alpine325"), "alpine") + expect_identical(codename_family("alpine324"), codename_family("alpine325")) + expect_false(identical( + codename_family("rhel9"), + codename_family("alpine324") + )) +}) diff --git a/local/uvr-install.sh b/local/uvr-install.sh index e103e13..f8c71eb 100755 --- a/local/uvr-install.sh +++ b/local/uvr-install.sh @@ -30,7 +30,7 @@ set -eu # renovate: datasource=github-releases depName=nbafrank/uvr -UVR_PIN="v0.4.7" +UVR_PIN="v0.4.8" if [ "$#" -eq 0 ]; then echo "usage: $0 ..." >&2 diff --git a/local/weekly-missing-binaries-audit.R b/local/weekly-missing-binaries-audit.R index e7ca776..c999e37 100644 --- a/local/weekly-missing-binaries-audit.R +++ b/local/weekly-missing-binaries-audit.R @@ -60,7 +60,8 @@ con <- DBI::dbConnect( port = 15432, user = "rpkgs", password = Sys.getenv("PGPASS"), - sslmode = "require" + sslmode = "verify-full", + sslrootcert = bincraft::postgres_sslrootcert("verify-full") ) errored_pkgs <- DBI::dbGetQuery( diff --git a/plans/2026-06-30-package-patching-implementation.md b/plans/2026-06-30-package-patching-implementation.md deleted file mode 100644 index 3a743d7..0000000 --- a/plans/2026-06-30-package-patching-implementation.md +++ /dev/null @@ -1,1477 +0,0 @@ -# Package Patching Implementation Plan - -> **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking. - -**Goal:** Let a curated registry of packages be "patched" (env/configure/Makevars overrides and/or source diffs) before `pak` installs them, including when they are transitive dependencies, so compiler-/OS-specific failures like `RcppParallel` stop cascading. - -**Architecture:** The mechanism lives in `bincraft`. Before `pak::local_install_deps()` runs, bincraft pre-builds each registry-matched package as a binary (reusing `pkgbuild::build(binary=TRUE)` + `cranlike::add_PACKAGES`), caches it, and serves it from a local `file://` repo prepended to `options("repos")` so `pak` installs the patched binary — direct or transitive — without recompiling. The curated registry (`registry.json` + diff files) lives in this repo and is passed in via a new `patches` argument. - -**Tech Stack:** R, `pak`, `pkgbuild`, `cranlike` (fork `pat-s/cranlike@s3`), `pkgsearch`, `withr`, `jsonlite`, `testthat` (3e), `mockery`. Two repos: `bincraft` at `/Users/pjs/git/codefloe.com/rpkgs/bincraftr`, and this repo `build-cran-binaries`. - -## Global Constraints - -- bincraft package name is `bincraft`; repo dir is `…/rpkgs/bincraftr`. Current version `4.2.2.9999`. -- Logging uses bincraft's wrappers only: `log_info`, `log_warn`, `log_success`, `log_debug`, `log_error`, `log_header` (never bare `message`/`cat` in package code). -- `cli`-style inline markup is allowed in log messages (e.g. `{.pkg %s}`, `{.path %s}`); curly braces in dynamic/error text must be escaped as already done in `install_helpers.R`. -- Tests: `testthat` 3rd edition, files at `tests/testthat/test-.R`, network/build tests guarded with `skip_on_cran()` / `skip_if_offline()`; end-to-end build tests guarded behind `skip_if_not(nzchar(Sys.getenv("BINCRAFT_PATCH_E2E")))`. -- New exported functions need roxygen with `@keywords internal` for non-user helpers; run `devtools::document()` after adding roxygen. -- Patched-binary cache dir default: `/mnt/cache/patched-binaries`. -- Platform tokens for matching = `c(, , )`, e.g. `ubuntu-2604` → `c("ubuntu-2604","ubuntu","amd64")`. -- Registry entry required fields: `package`, `versions`, `platforms`, `reason`. Optional: `env`, `configure_args`, `makevars`, `patch`. -- One sentence per line in prose/commit messages; do not hard-wrap at 80 columns. -- Use `fj -H codefloe.com` for any PR operations (Forgejo), not `gh`. - ---- - -## Phase A — bincraft mechanism - -All Phase A paths are relative to `/Users/pjs/git/codefloe.com/rpkgs/bincraftr`. - -### Task A0: Proof of mechanism — pak installs a patched binary from a prepended `file://` repo - -This de-risks the core assumption before building anything on top: that `pak` installs a binary from a local `file://` repo in preference to CRAN for an equal version, and does so without recompiling. If this fails, the contingency (documented in Step 4) is to serve patched *source* and rely on `pkgcache` build-caching — the rest of the plan changes only inside `build_patched_binary()`. - -**Files:** -- Create: `tools/verify-patch-mechanism.R` - -**Interfaces:** -- Produces: a runnable script proving `pak::pkg_install()` resolves a local patched binary over CRAN. No package API. - -- [ ] **Step 1: Write the verification script** - -```r -# tools/verify-patch-mechanism.R -# Proves pak installs a patched binary from a prepended file:// repo instead of -# CRAN's, without recompiling. Run inside a Linux build-env container: -# Rscript tools/verify-patch-mechanism.R -# Exits 0 on success, 1 on failure. - -pkg <- "glue" # small, pure-R CRAN package -sentinel <- "PatchMechanismProof" - -work <- tempfile("verify_") -repo <- file.path(work, "repo", "src", "contrib") -lib <- file.path(work, "lib") -dir.create(repo, recursive = TRUE) -dir.create(lib, recursive = TRUE) - -# 1. Download CRAN source for the current version. -ap <- available.packages(repos = "https://cloud.r-project.org") -ver <- ap[pkg, "Version"] -src <- file.path(work, sprintf("%s_%s.tar.gz", pkg, ver)) -download.file( - sprintf("https://cloud.r-project.org/src/contrib/%s_%s.tar.gz", pkg, ver), - src, mode = "wb" -) - -# 2. Unpack, inject a sentinel field into DESCRIPTION, build a binary. -untar(src, exdir = work) -desc <- file.path(work, pkg, "DESCRIPTION") -writeLines(c(readLines(desc), sprintf("%s: yes", sentinel)), desc) -pkgbuild::build( - file.path(work, pkg), binary = TRUE, vignettes = FALSE, - dest_path = repo, quiet = TRUE -) -built <- list.files(repo, pattern = sprintf("^%s_.*\\.tar\\.gz$", pkg), full.names = TRUE) -file.rename(built[1L], file.path(repo, sprintf("%s_%s.tar.gz", pkg, ver))) -cranlike::add_PACKAGES(sprintf("%s_%s.tar.gz", pkg, ver), repo) - -# 3. Install with the local repo prepended; assert our patched build won. -withr::with_options( - list(repos = c(patched = sprintf("file://%s", dirname(dirname(repo))), - CRAN = "https://cloud.r-project.org")), - pak::pkg_install(pkg, lib = lib, ask = FALSE, upgrade = FALSE) -) - -installed_desc <- file.path(lib, pkg, "DESCRIPTION") -ok <- file.exists(installed_desc) && - any(grepl(sentinel, readLines(installed_desc))) - -if (ok) { - cat("PROOF PASSED: pak installed the patched local binary.\n") - quit(status = 0L) -} else { - cat("PROOF FAILED: pak did not install the patched local binary.\n") - quit(status = 1L) -} -``` - -- [ ] **Step 2: Run the proof in a build-env container** - -Run (amd64 example; use any supported build-env image): - -```bash -just build-single ubuntu 2604 amd64 4.5.0 glue 1.0.0 1 || true # warms the env -docker run --rm -v "$PWD":/work -w /work reg.devxy.io/rpkgs/build-env-ubuntu:2604 \ - Rscript tools/verify-patch-mechanism.R -``` - -Expected: final line `PROOF PASSED: pak installed the patched local binary.` and exit status 0. - -- [ ] **Step 3: Commit** - -```bash -git add tools/verify-patch-mechanism.R -git commit -m "test(patches): prove pak installs a patched binary from a local file:// repo" -``` - -- [ ] **Step 4: Record the outcome / contingency** - -If the proof PASSED, proceed to Task A1 unchanged. -If it FAILED (pak recompiled or picked CRAN's), the mechanism switches to serving patched *source*: in Task A4 `build_patched_binary()` skips `pkgbuild::build()` and instead repackages the patched source tree with `pkgbuild::build(binary = FALSE)`; everything else (registry, matching, cache, repo prepend) is unchanged because `pak` build-caches the compiled result via `pkgcache`. Note the chosen path in the commit message and continue. - ---- - -### Task A1: Registry loading and normalization - -**Files:** -- Create: `R/patches.R` -- Test: `tests/testthat/test-patches.R` - -**Interfaces:** -- Produces: `load_patch_registry(patches_dir)` → `list()` of normalized entries; each entry is a named list with `package`, `versions`, `platforms` (character vector), `env` (named list), `configure_args` (character), `makevars` (named list), `reason`, and `patch_path` (absolute path or `NULL`). `normalize_patch_entry(entry, patches_dir)` → one normalized entry; errors on missing required field or missing patch file. - -- [ ] **Step 1: Write the failing test** - -```r -# tests/testthat/test-patches.R -test_that("load_patch_registry parses and normalizes entries", { - dir <- withr::local_tempdir() - writeLines("--- a patch ---", file.path(dir, "fix.patch")) - jsonlite::write_json( - list(list( - package = "RcppParallel", versions = "*", - platforms = list("alpine", "ubuntu-2604"), - env = list(RCPP_PARALLEL_USE_TBB = "0"), - patch = "fix.patch", reason = "bundled TBB fails" - )), - file.path(dir, "registry.json"), auto_unbox = TRUE - ) - - reg <- load_patch_registry(dir) - - expect_length(reg, 1L) - expect_identical(reg[[1L]]$package, "RcppParallel") - expect_identical(reg[[1L]]$platforms, c("alpine", "ubuntu-2604")) - expect_identical(reg[[1L]]$env$RCPP_PARALLEL_USE_TBB, "0") - expect_identical(reg[[1L]]$configure_args, character(0L)) - expect_true(file.exists(reg[[1L]]$patch_path)) -}) - -test_that("load_patch_registry returns empty list when no registry", { - expect_identical(load_patch_registry(NULL), list()) - expect_identical(load_patch_registry(withr::local_tempdir()), list()) -}) - -test_that("normalize_patch_entry errors on missing required field", { - expect_error( - normalize_patch_entry(list(package = "x"), tempdir()), - "missing required field" - ) -}) - -test_that("normalize_patch_entry errors on missing patch file", { - expect_error( - normalize_patch_entry( - list(package = "x", versions = "*", platforms = "alpine", - reason = "r", patch = "nope.patch"), - tempdir() - ), - "does not exist" - ) -}) -``` - -- [ ] **Step 2: Run test to verify it fails** - -Run: `Rscript -e 'devtools::load_all("."); testthat::test_file("tests/testthat/test-patches.R")'` -Expected: FAIL with "could not find function load_patch_registry". - -- [ ] **Step 3: Write minimal implementation** - -```r -# R/patches.R - -#' Load and validate the patch registry -#' -#' Reads `registry.json` from `patches_dir` and returns normalized entries. -#' -#' @param patches_dir Directory containing `registry.json` and any patch files, -#' or `NULL` to disable patching. -#' @return A list of normalized patch entries (possibly empty). -#' @keywords internal -load_patch_registry <- function(patches_dir) { - if (is.null(patches_dir)) { - return(list()) - } - registry_file <- file.path(patches_dir, "registry.json") - if (!file.exists(registry_file)) { - log_warn(sprintf( - "Patch directory {.path %s} has no registry.json; patching disabled.", - patches_dir - )) - return(list()) - } - raw <- jsonlite::fromJSON(registry_file, simplifyVector = FALSE) - lapply(raw, normalize_patch_entry, patches_dir = patches_dir) -} - -#' Normalize and validate a single patch registry entry -#' -#' @param entry A list parsed from `registry.json`. -#' @param patches_dir Directory used to resolve a relative `patch` path. -#' @return The entry with defaults filled and `patch_path` resolved. -#' @keywords internal -normalize_patch_entry <- function(entry, patches_dir) { - required <- c("package", "versions", "platforms", "reason") - missing <- setdiff(required, names(entry)) - if (length(missing) > 0L) { - stop( - sprintf("Patch entry is missing required field(s): %s", toString(missing)), - call. = FALSE - ) - } - entry$platforms <- as.character(unlist(entry$platforms)) - entry$env <- if (is.null(entry$env)) list() else entry$env - entry$configure_args <- if (is.null(entry$configure_args)) { - character(0L) - } else { - as.character(unlist(entry$configure_args)) - } - entry$makevars <- if (is.null(entry$makevars)) list() else entry$makevars - if (!is.null(entry$patch)) { - patch_path <- file.path(patches_dir, entry$patch) - if (!file.exists(patch_path)) { - stop( - sprintf( - "Patch file '%s' for package '%s' does not exist.", - patch_path, entry$package - ), - call. = FALSE - ) - } - entry$patch_path <- patch_path - } else { - entry$patch_path <- NULL - } - entry -} -``` - -- [ ] **Step 4: Run test to verify it passes** - -Run: `Rscript -e 'devtools::load_all("."); testthat::test_file("tests/testthat/test-patches.R")'` -Expected: PASS (4 tests). - -- [ ] **Step 5: Commit** - -```bash -git add R/patches.R tests/testthat/test-patches.R -git commit -m "feat(patches): load and validate the patch registry" -``` - ---- - -### Task A2: Platform matching and version-constraint satisfaction - -**Files:** -- Modify: `R/patches.R` -- Test: `tests/testthat/test-patches.R` - -**Interfaces:** -- Consumes: normalized entries from Task A1. -- Produces: `build_platform_tokens(platform, arch)` → character vector; `entry_matches_platform(entry, tokens)` → logical; `match_patch_entries(registry, platform, arch)` → filtered list; `version_satisfies(version, constraint)` → logical (constraint forms: `"*"` handled by caller, `"x.y.z"` exact, `">=x"`, `"<=x"`, `">x"`, `"=5.1.0")) - expect_false(version_satisfies("5.0.0", ">=5.1.0")) - expect_true(version_satisfies("5.1.11-2", "<=5.1.11-2")) - expect_false(version_satisfies("5.1.12", "<=5.1.11-2")) -}) -``` - -- [ ] **Step 2: Run test to verify it fails** - -Run: `Rscript -e 'devtools::load_all("."); testthat::test_file("tests/testthat/test-patches.R")'` -Expected: FAIL with "could not find function build_platform_tokens". - -- [ ] **Step 3: Write minimal implementation (append to `R/patches.R`)** - -```r -#' Build platform tokens for patch matching -#' @keywords internal -build_platform_tokens <- function(platform, arch) { - family <- sub("-.*$", "", platform) - unique(c(platform, family, arch)) -} - -#' Does a patch entry apply to the current platform tokens? -#' @keywords internal -entry_matches_platform <- function(entry, tokens) { - any(entry$platforms == "*") || - length(intersect(entry$platforms, tokens)) > 0L -} - -#' Filter registry entries applicable to the current build -#' @keywords internal -match_patch_entries <- function(registry, platform, arch) { - if (length(registry) == 0L) { - return(list()) - } - tokens <- build_platform_tokens(platform, arch) - Filter(function(e) entry_matches_platform(e, tokens), registry) -} - -#' Test whether a version satisfies a single constraint -#' -#' @param version A version string (CRAN style, may contain `-`). -#' @param constraint One of `"x.y.z"`, `"==x"`, `">=x"`, `"<=x"`, `">x"`, `"=|<=|==|>|<)?\\s*(.+)$", constraint) - )[[1L]] - op <- parts[2L] - target <- parts[3L] - v <- package_version(version) - t <- package_version(target) - if (op == "" || op == "==") { - return(v == t) - } - switch( - op, - ">=" = v >= t, - "<=" = v <= t, - ">" = v > t, - "<" = v < t, - FALSE - ) -} -``` - -- [ ] **Step 4: Run test to verify it passes** - -Run: `Rscript -e 'devtools::load_all("."); testthat::test_file("tests/testthat/test-patches.R")'` -Expected: PASS. - -- [ ] **Step 5: Commit** - -```bash -git add R/patches.R tests/testthat/test-patches.R -git commit -m "feat(patches): platform matching and version-constraint checks" -``` - ---- - -### Task A3: Cache key and version resolution - -**Files:** -- Modify: `R/patches.R` -- Test: `tests/testthat/test-patches.R` - -**Interfaces:** -- Consumes: normalized entries. -- Produces: `patch_cache_key(entry, version, platform, arch, r_minor)` → string `"_____"`, where `hash12` covers `env`/`configure_args`/`makevars`/patch bytes; `resolve_patch_version(entry)` → latest CRAN version satisfying `entry$versions`, or `NA_character_`; `describe_patch(entry)` → short human label. - -- [ ] **Step 1: Write the failing test** - -```r -test_that("patch_cache_key is stable and sensitive to env/patch changes", { - e1 <- list(package = "P", env = list(A = "1"), - configure_args = character(0L), makevars = list(), - patch_path = NULL) - e2 <- e1; e2$env <- list(A = "2") - - k1 <- patch_cache_key(e1, "1.0", "alpine-324", "amd64", "4.5") - expect_identical(k1, patch_cache_key(e1, "1.0", "alpine-324", "amd64", "4.5")) - expect_false(identical( - k1, patch_cache_key(e2, "1.0", "alpine-324", "amd64", "4.5") - )) - expect_match(k1, "^P_1.0_alpine-324_amd64_4.5_[0-9a-f]{12}$") -}) - -test_that("resolve_patch_version returns latest for wildcard, NA when unmet", { - local_mocked_bindings( - cran_package = function(pkg) list(Version = "5.1.12"), - .package = "pkgsearch" - ) - expect_identical( - resolve_patch_version(list(package = "RcppParallel", versions = "*")), - "5.1.12" - ) - expect_identical( - resolve_patch_version(list(package = "RcppParallel", versions = ">=9.0")), - NA_character_ - ) -}) - -test_that("describe_patch summarizes the active overrides", { - expect_match( - describe_patch(list(env = list(RCPP_PARALLEL_USE_TBB = "0"), - configure_args = character(0L), makevars = list(), - patch_path = NULL)), - "env: RCPP_PARALLEL_USE_TBB=0" - ) - expect_match( - describe_patch(list(env = list(), configure_args = character(0L), - makevars = list(), patch_path = "/x/fix.patch")), - "source patch" - ) -}) -``` - -- [ ] **Step 2: Run test to verify it fails** - -Run: `Rscript -e 'devtools::load_all("."); testthat::test_file("tests/testthat/test-patches.R")'` -Expected: FAIL with "could not find function patch_cache_key". - -- [ ] **Step 3: Write minimal implementation (append to `R/patches.R`)** - -```r -#' Compute the cache key for a patched binary -#' @keywords internal -patch_cache_key <- function(entry, version, platform, arch, r_minor) { - payload <- list( - env = entry$env, - configure_args = entry$configure_args, - makevars = entry$makevars, - patch = if (!is.null(entry$patch_path)) { - readBin(entry$patch_path, "raw", file.size(entry$patch_path)) - } else { - raw(0L) - } - ) - tmp <- tempfile() - on.exit(unlink(tmp), add = TRUE) - saveRDS(payload, tmp) - hash <- substr(unname(tools::md5sum(tmp)), 1L, 12L) - sprintf( - "%s_%s_%s_%s_%s_%s", - entry$package, version, platform, arch, r_minor, hash - ) -} - -#' Resolve the CRAN version to build for a patch entry -#' -#' Returns the latest CRAN version satisfying the entry's `versions` constraint, -#' or `NA_character_` when CRAN's latest does not satisfy it or lookup fails. -#' @keywords internal -resolve_patch_version <- function(entry) { - latest <- tryCatch( - pkgsearch::cran_package(entry$package)$Version, - error = function(e) NA_character_ - ) - if (is.na(latest)) { - return(NA_character_) - } - if (identical(entry$versions, "*") || version_satisfies(latest, entry$versions)) { - return(latest) - } - NA_character_ -} - -#' Short human label describing a patch entry's overrides -#' @keywords internal -describe_patch <- function(entry) { - bits <- character(0L) - if (length(entry$env) > 0L) { - bits <- c(bits, sprintf( - "env: %s", - paste( - names(entry$env), - unlist(entry$env), - sep = "=", collapse = "," - ) - )) - } - if (length(entry$configure_args) > 0L) { - bits <- c(bits, sprintf("configure: %s", toString(entry$configure_args))) - } - if (length(entry$makevars) > 0L) { - bits <- c(bits, "makevars") - } - if (!is.null(entry$patch_path)) { - bits <- c(bits, "source patch") - } - if (length(bits) == 0L) "no-op" else paste(bits, collapse = "; ") -} -``` - -- [ ] **Step 4: Run test to verify it passes** - -Run: `Rscript -e 'devtools::load_all("."); testthat::test_file("tests/testthat/test-patches.R")'` -Expected: PASS. (Note: `local_mocked_bindings` requires testthat >= 3.1.7; bincraft uses 3e.) - -- [ ] **Step 5: Commit** - -```bash -git add R/patches.R tests/testthat/test-patches.R -git commit -m "feat(patches): cache key, version resolution, and patch description" -``` - ---- - -### Task A4: Build a patched binary in isolation - -**Files:** -- Modify: `R/patches.R` -- Test: `tests/testthat/test-patches.R` - -**Interfaces:** -- Consumes: a normalized entry, a resolved `version`, a `dest_dir`. -- Produces: `download_cran_source(package, version, dest_dir, cran)` → path or `NULL`; `apply_source_patch(patch_path, pkg_src)` → logical; `configure_args_to_build_args(configure_args)` → character; `build_patched_binary(entry, version, dest_dir)` → path to built binary tarball or `NULL`. - -- [ ] **Step 1: Write the failing test** - -```r -test_that("configure_args_to_build_args formats configure args", { - expect_identical(configure_args_to_build_args(character(0L)), character(0L)) - expect_identical( - configure_args_to_build_args(c("--with-foo", "--no-bar")), - "--configure-args=--with-foo --no-bar" - ) -}) - -test_that("apply_source_patch returns FALSE when patch does not apply", { - src <- withr::local_tempdir() - writeLines("unrelated content", file.path(src, "file.txt")) - bad_patch <- tempfile(fileext = ".patch") - writeLines(c( - "--- a/missing.txt", "+++ b/missing.txt", - "@@ -1 +1 @@", "-nope", "+nope2" - ), bad_patch) - expect_false(apply_source_patch(bad_patch, src)) -}) - -test_that("build_patched_binary returns NULL when download fails", { - local_mocked_bindings(download_cran_source = function(...) NULL) - expect_null( - build_patched_binary( - list(package = "P", env = list(), configure_args = character(0L), - makevars = list(), patch_path = NULL), - "1.0", withr::local_tempdir() - ) - ) -}) -``` - -- [ ] **Step 2: Run test to verify it fails** - -Run: `Rscript -e 'devtools::load_all("."); testthat::test_file("tests/testthat/test-patches.R")'` -Expected: FAIL with "could not find function configure_args_to_build_args". - -- [ ] **Step 3: Write minimal implementation (append to `R/patches.R`)** - -```r -#' Download a CRAN source tarball for an exact version -#' @keywords internal -download_cran_source <- function( - package, - version, - dest_dir, - cran = "https://cloud.r-project.org" -) { - fname <- sprintf("%s_%s.tar.gz", package, version) - urls <- c( - sprintf("%s/src/contrib/%s", cran, fname), - sprintf("%s/src/contrib/Archive/%s/%s", cran, package, fname) - ) - dest <- file.path(dest_dir, fname) - for (u in urls) { - ok <- tryCatch( - { - utils::download.file(u, dest, mode = "wb", quiet = TRUE) - file.exists(dest) && file.size(dest) > 0L - }, - error = function(e) FALSE - ) - if (isTRUE(ok)) { - return(dest) - } - } - log_warn(sprintf( - "Could not download CRAN source for {.pkg %s} %s.", - package, version - )) - NULL -} - -#' Apply a unified diff to an unpacked source tree -#' -#' Uses `patch -p1 --forward` so an already-applied or non-applying patch fails -#' cleanly (returns FALSE) instead of corrupting the tree. -#' @keywords internal -apply_source_patch <- function(patch_path, pkg_src) { - status <- system2( - "patch", - args = c( - "-p1", "--forward", "--batch", - "-d", shQuote(pkg_src), - "-i", shQuote(patch_path) - ), - stdout = FALSE, stderr = FALSE - ) - identical(status, 0L) -} - -#' Format configure args for `pkgbuild::build(args = ...)` -#' @keywords internal -configure_args_to_build_args <- function(configure_args) { - if (length(configure_args) == 0L) { - return(character(0L)) - } - sprintf("--configure-args=%s", paste(configure_args, collapse = " ")) -} - -#' Build a patched binary for one registry entry, in isolation -#' -#' Downloads CRAN source for `version`, applies the source patch (if any), and -#' builds a binary with the entry's env / configure / Makevars overrides scoped -#' to this build only. Returns the built tarball path, or `NULL` on any failure. -#' @keywords internal -build_patched_binary <- function(entry, version, dest_dir) { - workdir <- tempfile("patch_build_") - dir.create(workdir, recursive = TRUE, showWarnings = FALSE) - on.exit(unlink(workdir, recursive = TRUE, force = TRUE), add = TRUE) - - src_tarball <- download_cran_source(entry$package, version, workdir) - if (is.null(src_tarball)) { - return(NULL) - } - - utils::untar(src_tarball, exdir = workdir) - pkg_src <- file.path(workdir, entry$package) - - if (!is.null(entry$patch_path)) { - if (!apply_source_patch(entry$patch_path, pkg_src)) { - log_warn(sprintf( - "Patch for {.pkg %s} %s did not apply cleanly; skipping patched build.", - entry$package, version - )) - return(NULL) - } - } - - build_env <- entry$env - if (length(entry$makevars) > 0L) { - mk <- tempfile(fileext = ".mk") - writeLines( - vapply( - names(entry$makevars), - function(k) sprintf("%s=%s", k, entry$makevars[[k]]), - character(1L) - ), - mk - ) - build_env$R_MAKEVARS_USER <- mk - } - - tryCatch( - withr::with_envvar(build_env, { - pkgbuild::build( - path = pkg_src, - binary = TRUE, - vignettes = FALSE, - dest_path = dest_dir, - args = configure_args_to_build_args(entry$configure_args), - quiet = TRUE - ) - }), - error = function(e) { - log_warn(sprintf( - "Isolated patched build of {.pkg %s} %s failed: %s", - entry$package, version, conditionMessage(e) - )) - NULL - } - ) -} -``` - -- [ ] **Step 4: Run test to verify it passes** - -Run: `Rscript -e 'devtools::load_all("."); testthat::test_file("tests/testthat/test-patches.R")'` -Expected: PASS. - -- [ ] **Step 5: Commit** - -```bash -git add R/patches.R tests/testthat/test-patches.R -git commit -m "feat(patches): build a patched binary in isolation from CRAN source" -``` - ---- - -### Task A5: Orchestrate the local patched repo (cache + index) - -**Files:** -- Modify: `R/patches.R` -- Test: `tests/testthat/test-patches.R` - -**Interfaces:** -- Consumes: all helpers above. -- Produces: `prepare_patched_repo(patches_dir, platform, arch, r_minor, cache_dir, repo_dir)` → path to a `src/contrib`-style dir containing patched binaries + a `PACKAGES` index, or `NULL` when nothing matched/built. On a cache hit it copies the cached tarball into `repo_dir`; on a miss it builds, then writes the result into `cache_dir`. - -- [ ] **Step 1: Write the failing test** - -```r -test_that("prepare_patched_repo returns NULL when no entries match", { - dir <- withr::local_tempdir() - jsonlite::write_json( - list(list(package = "A", versions = "*", platforms = list("redhat"), - reason = "r")), - file.path(dir, "registry.json"), auto_unbox = TRUE - ) - expect_null( - prepare_patched_repo(dir, "ubuntu-2604", "amd64", "4.5", - cache_dir = withr::local_tempdir(), - repo_dir = withr::local_tempdir()) - ) -}) - -test_that("prepare_patched_repo serves a cached binary and writes an index", { - dir <- withr::local_tempdir() - jsonlite::write_json( - list(list(package = "glue", versions = "*", platforms = list("*"), - env = list(A = "1"), reason = "r")), - file.path(dir, "registry.json"), auto_unbox = TRUE - ) - cache <- withr::local_tempdir() - repo <- withr::local_tempdir() - - local_mocked_bindings( - resolve_patch_version = function(entry) "1.0.0", - build_patched_binary = function(entry, version, dest_dir) { - f <- file.path(dest_dir, sprintf("%s_%s.tar.gz", entry$package, version)) - writeLines("fake binary", f) - f - } - ) - - out <- prepare_patched_repo(dir, "ubuntu-2604", "amd64", "4.5", - cache_dir = cache, repo_dir = repo) - - expect_identical(out, repo) - expect_true(file.exists(file.path(repo, "glue_1.0.0.tar.gz"))) - expect_true(file.exists(file.path(repo, "PACKAGES"))) - # The build result was cached under the key. - expect_length(list.files(cache, pattern = "^glue_1.0.0_.*\\.tar\\.gz$"), 1L) -}) -``` - -- [ ] **Step 2: Run test to verify it fails** - -Run: `Rscript -e 'devtools::load_all("."); testthat::test_file("tests/testthat/test-patches.R")'` -Expected: FAIL with "could not find function prepare_patched_repo". - -- [ ] **Step 3: Write minimal implementation (append to `R/patches.R`)** - -```r -#' Prepare a local repo of patched binaries for the current build -#' -#' For each registry entry matching the current platform, ensures a patched -#' binary is present in `repo_dir` (from `cache_dir` if available, else built -#' and then cached) and writes a `PACKAGES` index over them. -#' -#' @param patches_dir Directory with `registry.json`, or `NULL`. -#' @param platform Build platform, e.g. `"ubuntu-2604"`. -#' @param arch Build arch, e.g. `"amd64"`. -#' @param r_minor R `"major.minor"` string, e.g. `"4.5"`. -#' @param cache_dir Persistent cache for patched binaries. -#' @param repo_dir Directory to assemble the local repo in. -#' @return `repo_dir` if at least one patched binary was produced, else `NULL`. -#' @keywords internal -prepare_patched_repo <- function( - patches_dir, - platform, - arch, - r_minor, - cache_dir = file.path("/mnt", "cache", "patched-binaries"), - repo_dir = tempfile("patched_repo_") -) { - entries <- match_patch_entries( - load_patch_registry(patches_dir), platform, arch - ) - if (length(entries) == 0L) { - return(NULL) - } - - dir.create(repo_dir, recursive = TRUE, showWarnings = FALSE) - dir.create(cache_dir, recursive = TRUE, showWarnings = FALSE) - - produced <- 0L - for (entry in entries) { - version <- resolve_patch_version(entry) - if (is.na(version)) { - log_warn(sprintf( - "No CRAN version of {.pkg %s} satisfies '%s'; patch skipped.", - entry$package, entry$versions - )) - next - } - - key <- patch_cache_key(entry, version, platform, arch, r_minor) - cached <- file.path(cache_dir, sprintf("%s.tar.gz", key)) - target <- file.path( - repo_dir, sprintf("%s_%s.tar.gz", entry$package, version) - ) - - if (file.exists(cached)) { - log_info(sprintf( - "Using cached patched binary for {.pkg %s} %s.", - entry$package, version - )) - file.copy(cached, target, overwrite = TRUE) - } else { - log_info(sprintf( - "Applying patch to {.pkg %s} %s [%s]: %s", - entry$package, version, describe_patch(entry), entry$reason - )) - built <- build_patched_binary(entry, version, repo_dir) - if (is.null(built)) { - next - } - if (!identical(normalizePath(built), normalizePath(target))) { - file.copy(built, target, overwrite = TRUE) - } - file.copy(target, cached, overwrite = TRUE) - } - produced <- produced + 1L - } - - if (produced == 0L) { - return(NULL) - } - cranlike::add_PACKAGES( - list.files(repo_dir, pattern = "\\.tar\\.gz$"), - repo_dir - ) - repo_dir -} -``` - -- [ ] **Step 4: Run test to verify it passes** - -Run: `Rscript -e 'devtools::load_all("."); testthat::test_file("tests/testthat/test-patches.R")'` -Expected: PASS. - -- [ ] **Step 5: Update imports and document** - -Add to `DESCRIPTION` `Imports:` (alphabetical) `jsonlite` if not present, and ensure `pkgsearch`, `pkgbuild`, `cranlike`, `withr` are listed (they are). Then: - -```bash -Rscript -e 'devtools::document()' -git add R/patches.R tests/testthat/test-patches.R DESCRIPTION NAMESPACE -git commit -m "feat(patches): orchestrate local patched-binary repo with caching" -``` - ---- - -### Task A6: Wire patched repo into the pak install path - -**Files:** -- Modify: `R/install_helpers.R:333-389` (`run_pak_install_with_mutex`) -- Modify: `R/install-deps.R:23-75` (`install_pkg_sys_deps`) -- Test: `tests/testthat/test-patches.R` - -**Interfaces:** -- Consumes: `prepare_patched_repo()`. -- Produces: `run_pak_install_with_mutex(local_clone_dir_single, env_vars, patched_repo = NULL)` — prepends `file://` to `options("repos")` for the install; `install_pkg_sys_deps(package_name, tag, local_clone_dir, platform, aggressive_cleanup = FALSE, patches = NULL, arch = NULL)` — builds the patched repo before installing. - -- [ ] **Step 1: Write the failing test** - -```r -test_that("run_pak_install_with_mutex prepends the patched repo to repos", { - seen <- NULL - local_mocked_bindings( - acquire_pak_mutex = function(...) tempfile(), - release_pak_mutex = function(...) invisible(NULL), - retry_with_backoff = function(func, ...) func() - ) - local_mocked_bindings( - local_install_deps = function(...) { - seen <<- getOption("repos") - invisible(TRUE) - }, - .package = "pak" - ) - - run_pak_install_with_mutex( - tempfile(), list(), patched_repo = "/tmp/patched" - ) - - expect_true(any(grepl("file:///tmp/patched", seen))) -}) -``` - -- [ ] **Step 2: Run test to verify it fails** - -Run: `Rscript -e 'devtools::load_all("."); testthat::test_file("tests/testthat/test-patches.R")'` -Expected: FAIL — `patched_repo` argument not yet accepted / repos not prepended. - -- [ ] **Step 3: Edit `run_pak_install_with_mutex` in `R/install_helpers.R`** - -Change the signature line: - -```r -run_pak_install_with_mutex <- function(local_clone_dir_single, env_vars) { -``` - -to: - -```r -run_pak_install_with_mutex <- function( - local_clone_dir_single, - env_vars, - patched_repo = NULL -) { -``` - -Replace the inner `retry_with_backoff(...)` block (the one wrapping `pak::local_install_deps`) with: - -```r - retry_with_backoff(function() { - withr::with_envvar(env_vars, { - repos <- getOption("repos") - if (!is.null(patched_repo)) { - repos <- c( - patched = sprintf("file://%s", patched_repo), - repos - ) - } - withr::with_options(list(repos = repos), { - # Default to non-verbose (suppressed messages) - suppressMessages(pak::local_install_deps(sprintf( - "%s", - local_clone_dir_single - ))) - }) - }) - }) -``` - -- [ ] **Step 4: Edit `install_pkg_sys_deps` in `R/install-deps.R`** - -Change the signature to add `patches` and `arch`: - -```r -install_pkg_sys_deps <- function( - package_name, - tag, - local_clone_dir, - platform = platform, - aggressive_cleanup = FALSE, - patches = NULL, - arch = NULL -) { -``` - -Immediately before the `run_pak_install_with_mutex(...)` call, insert: - -```r - # Build a local repo of patched binaries (if any apply) and serve it to pak. - r_minor <- paste( - R.version$major, - strsplit(R.version$minor, ".", fixed = TRUE)[[1L]][1L], - sep = "." - ) - patched_repo <- tryCatch( - prepare_patched_repo(patches, platform, arch, r_minor), - error = function(e) { - log_warn(sprintf("Patch preparation failed: %s", conditionMessage(e))) - NULL - } - ) -``` - -and change the call from: - -```r - run_pak_install_with_mutex( - local_clone_dir_single, - env_vars - ) -``` - -to: - -```r - run_pak_install_with_mutex( - local_clone_dir_single, - env_vars, - patched_repo = patched_repo - ) -``` - -- [ ] **Step 5: Run test to verify it passes** - -Run: `Rscript -e 'devtools::load_all("."); testthat::test_file("tests/testthat/test-patches.R")'` -Expected: PASS. - -- [ ] **Step 6: Commit** - -```bash -git add R/install_helpers.R R/install-deps.R tests/testthat/test-patches.R -git commit -m "feat(patches): serve patched binaries to pak during dep install" -``` - ---- - -### Task A7: Thread `patches` through the public build API - -**Files:** -- Modify: `R/build_binaries.R` (`build_binary_package`, `execute_package_builds`, `build_single_tag`, `handle_system_dependencies`) -- Create: `man-roxygen/param-patches.R` -- Test: `tests/testthat/test-patches.R` - -**Interfaces:** -- Produces: `build_binary_package(..., patches = NULL)` and the internal chain each carry `patches` down to `install_pkg_sys_deps()`. `handle_system_dependencies(..., patches = NULL)` passes `patches` and `arch` through. - -- [ ] **Step 1: Write the failing test** - -```r -test_that("handle_system_dependencies forwards patches and arch", { - captured <- list() - local_mocked_bindings( - install_pkg_sys_deps = function(package_name, tag, local_clone_dir_single, - platform, patches = NULL, arch = NULL) { - captured <<- list(patches = patches, arch = arch) - invisible(TRUE) - } - ) - handle_system_dependencies( - "RcppParallel", "5.1.11-2", "ubuntu-2604", tempfile(), "amd64", - NULL, NULL, NULL, NULL, NULL, NULL, NULL, - patches = "local/patches" - ) - expect_identical(captured$patches, "local/patches") - expect_identical(captured$arch, "amd64") -}) -``` - -- [ ] **Step 2: Run test to verify it fails** - -Run: `Rscript -e 'devtools::load_all("."); testthat::test_file("tests/testthat/test-patches.R")'` -Expected: FAIL — `handle_system_dependencies` has no `patches` argument. - -- [ ] **Step 3: Create the roxygen template** - -```r -# man-roxygen/param-patches.R -#' @param patches Optional path to a patch registry directory containing a -#' `registry.json` (and any referenced diff files). When set, matching -#' packages are pre-built as patched binaries and served to `pak` during -#' dependency installation. Defaults to `NULL` (no patching). -``` - -- [ ] **Step 4: Edit the four functions in `R/build_binaries.R`** - -In `build_single_tag()`'s call to `handle_system_dependencies(...)`, add `patches = patches` as the final argument, and add `patches = NULL` to `build_single_tag`'s own signature plus `#' @template param-patches` to its roxygen block. - -Change `handle_system_dependencies` signature to end with `metadata_db_sslmode,` then add `patches = NULL`, and change its inner `install_pkg_sys_deps(...)` call from: - -```r - install_pkg_sys_deps( - package_name, - tag, - local_clone_dir_single, - platform - ) -``` - -to: - -```r - install_pkg_sys_deps( - package_name, - tag, - local_clone_dir_single, - platform, - patches = patches, - arch = arch - ) -``` - -In `execute_package_builds()`, add `patches = NULL` to the signature and pass `patches = patches` into its `build_single_tag(...)` call inside `worker_function`. - -In `build_binary_package()`, add `patches = NULL` to the signature (after `s3_package_cache`), add `#' @template param-patches` to its roxygen, and pass `patches = patches` into the `execute_package_builds(...)` call. - -- [ ] **Step 5: Document, test, and run package check** - -Run: - -```bash -Rscript -e 'devtools::document()' -Rscript -e 'devtools::load_all("."); testthat::test_file("tests/testthat/test-patches.R")' -``` - -Expected: PASS for the new test; `man/build_binary_package.Rd` etc. regenerated. - -- [ ] **Step 6: Commit** - -```bash -git add R/build_binaries.R man-roxygen/param-patches.R man NAMESPACE tests/testthat/test-patches.R -git commit -m "feat(patches): thread patches argument through build_binary_package" -``` - ---- - -### Task A8: End-to-end patch test (guarded) and version bump - -**Files:** -- Modify: `tests/testthat/test-patches.R` -- Modify: `DESCRIPTION` (version), `NEWS.md` - -**Interfaces:** -- Produces: a guarded e2e test proving a dependent package builds when its failing dependency is patched. - -- [ ] **Step 1: Add the guarded e2e test** - -```r -test_that("a patched dependency unblocks a dependent build (e2e)", { - skip_if_not(nzchar(Sys.getenv("BINCRAFT_PATCH_E2E"))) - skip_if_offline() - - patches_dir <- withr::local_tempdir() - jsonlite::write_json( - list(list( - package = "RcppParallel", versions = "*", platforms = list("*"), - env = list(RCPP_PARALLEL_USE_TBB = "0"), - reason = "bundled TBB fails on this toolchain" - )), - file.path(patches_dir, "registry.json"), auto_unbox = TRUE - ) - - out <- withr::local_tempdir() - result <- build_binary_package( - "rts2", tag = "latest", local_output_dir_root = out, - upload = FALSE, archive = FALSE, patches = patches_dir - ) - expect_true(isTRUE(result) || identical(result, "skipped")) -}) -``` - -- [ ] **Step 2: Run the e2e test in a container** - -Run: - -```bash -docker run --rm -e BINCRAFT_PATCH_E2E=1 -v "$PWD":/work -w /work \ - reg.devxy.io/rpkgs/build-env-ubuntu:2604 \ - Rscript -e 'devtools::load_all("."); testthat::test_file("tests/testthat/test-patches.R")' -``` - -Expected: the e2e test runs (not skipped) and PASSES; build log shows `Applying patch to RcppParallel`. - -- [ ] **Step 3: Bump version and changelog** - -In `DESCRIPTION`, bump `Version:` to `4.3.0.9999`. Prepend to `NEWS.md`: - -```markdown -# bincraft 4.3.0 - -* `build_binary_package()` gains a `patches` argument: a registry of - per-package env / configure / Makevars overrides and source diffs that are - pre-built into patched binaries and served to pak, fixing compiler- and - OS-specific failures (e.g. RcppParallel) including for transitive deps. -``` - -- [ ] **Step 4: Commit** - -```bash -git add tests/testthat/test-patches.R DESCRIPTION NEWS.md -git commit -m "test(patches): guarded end-to-end test; bump to 4.3.0.9999" -``` - ---- - -## Phase B — build-cran-binaries registry and wiring - -All Phase B paths are relative to this repo (`build-cran-binaries`). Phase B depends on a bincraft build that includes Phase A (install the updated bincraft in the build containers, or bump the pinned version in `.crow/build-all-versions-install-deps.yaml`). - -### Task B1: Create the patch registry with the RcppParallel entry - -**Files:** -- Create: `local/patches/registry.json` -- Create: `local/patches/README.md` - -**Interfaces:** -- Produces: the curated registry consumed by bincraft's `patches` argument. - -- [ ] **Step 1: Write the registry** - -```json -[ - { - "package": "RcppParallel", - "versions": "*", - "platforms": ["alpine", "ubuntu-2604"], - "env": { "RCPP_PARALLEL_USE_TBB": "0" }, - "configure_args": [], - "makevars": {}, - "patch": null, - "reason": "bundled Intel TBB fails to build on musl and on newer toolchains (e.g. g++ 15 on ubuntu-2604); disabling TBB falls back to TinyThread" - } -] -``` - -- [ ] **Step 2: Write the README** - -`local/patches/README.md` documents the schema (copy the field table from `specs/2026-06-30-package-patching-design.md`), how to add an entry, and that source diffs go in `local/patches//.patch` referenced by the `patch` field. - -- [ ] **Step 3: Verify it parses** - -Run: `Rscript -e 'x <- jsonlite::fromJSON("local/patches/registry.json", simplifyVector = FALSE); stopifnot(length(x) == 1L, x[[1]]$package == "RcppParallel"); cat("ok\n")'` -Expected: `ok`. - -- [ ] **Step 4: Commit** - -```bash -git add local/patches/registry.json local/patches/README.md -git commit -m "feat(patches): add patch registry with RcppParallel TBB workaround" -``` - ---- - -### Task B2: Registry validator script - -**Files:** -- Create: `local/validate-patches.R` - -**Interfaces:** -- Consumes: `local/patches/registry.json`. -- Produces: a script that exits non-zero on schema violations, missing patch files, or ambiguous overlapping entries. - -- [ ] **Step 1: Write the validator** - -```r -#!/usr/bin/env Rscript -# Validate local/patches/registry.json: schema, referenced patch files, and -# ambiguous overlaps. Exits 1 on any problem. Used by pre-commit and CI. - -dir <- "local/patches" -registry_file <- file.path(dir, "registry.json") -if (!file.exists(registry_file)) { - cat("No registry.json found; nothing to validate.\n") - quit(status = 0L) -} - -reg <- jsonlite::fromJSON(registry_file, simplifyVector = FALSE) -required <- c("package", "versions", "platforms", "reason") -errs <- character(0L) - -for (i in seq_along(reg)) { - e <- reg[[i]] - missing <- setdiff(required, names(e)) - if (length(missing) > 0L) { - errs <- c(errs, sprintf( - "entry %d (%s): missing %s", i, - if (is.null(e$package)) "?" else e$package, toString(missing) - )) - } - if (!is.null(e$patch)) { - p <- file.path(dir, e$patch) - if (!file.exists(p)) { - errs <- c(errs, sprintf("entry %d (%s): patch file '%s' missing", - i, e$package, p)) - } - } -} - -# Ambiguous overlap: two entries for the same package with identical platforms -# and versions. -keys <- vapply(reg, function(e) { - sprintf("%s|%s|%s", e$package, - paste(sort(as.character(unlist(e$platforms))), collapse = ","), - e$versions) -}, character(1L)) -dups <- keys[duplicated(keys)] -if (length(dups) > 0L) { - errs <- c(errs, sprintf("ambiguous duplicate entries: %s", toString(unique(dups)))) -} - -if (length(errs) > 0L) { - cat("Patch registry validation FAILED:\n") - cat(paste0(" - ", errs, "\n")) - quit(status = 1L) -} -cat(sprintf("Patch registry OK (%d entrie(s)).\n", length(reg))) -``` - -- [ ] **Step 2: Run it (expect success on the B1 registry)** - -Run: `Rscript local/validate-patches.R` -Expected: `Patch registry OK (1 entrie(s)).` and exit 0. - -- [ ] **Step 3: Run it against a broken registry (expect failure)** - -Run: - -```bash -cp local/patches/registry.json /tmp/reg.bak -Rscript -e 'writeLines("[{\"package\":\"X\"}]", "local/patches/registry.json")' -Rscript local/validate-patches.R; echo "exit=$?" -cp /tmp/reg.bak local/patches/registry.json -``` - -Expected: prints `validation FAILED` with a missing-field message and `exit=1`. - -- [ ] **Step 4: Commit** - -```bash -git add local/validate-patches.R -git commit -m "feat(patches): add registry validator script" -``` - ---- - -### Task B3: Hook the validator into pre-commit - -**Files:** -- Modify: `.pre-commit-config.yaml` - -**Interfaces:** -- Produces: a local hook that runs `local/validate-patches.R` when the registry or patch files change. - -- [ ] **Step 1: Add the hook** - -Add a `repo: local` hook entry to `.pre-commit-config.yaml`: - -```yaml - - repo: local - hooks: - - id: validate-patches - name: validate patch registry - entry: Rscript local/validate-patches.R - language: system - files: ^local/patches/ - pass_filenames: false -``` - -- [ ] **Step 2: Verify the hook runs** - -Run: `pre-commit run validate-patches --all-files` -Expected: hook passes (`Patch registry OK`). - -- [ ] **Step 3: Commit** - -```bash -git add .pre-commit-config.yaml -git commit -m "ci(patches): validate patch registry in pre-commit" -``` - ---- - -### Task B4: Pass `patches` through the build entry points - -**Files:** -- Modify: `local/build-one.R:99-119` (the `build_binary_package` call) -- Modify: `local/build-all.R:125-145` (the `build_binary_package` call) - -**Interfaces:** -- Consumes: the new bincraft `patches` argument (Phase A) and `local/patches/`. -- Produces: both entry points pass `patches = "local/patches"`. - -- [ ] **Step 1: Edit `local/build-one.R`** - -In the `bincraft::build_binary_package(` call, add as a new argument (e.g. after `archive = TRUE,`): - -```r - patches = "local/patches", -``` - -- [ ] **Step 2: Edit `local/build-all.R`** - -In the `bincraft::build_binary_package(` call, add: - -```r - patches = "local/patches", -``` - -- [ ] **Step 3: Verify the scripts still parse** - -Run: `Rscript -e 'invisible(parse("local/build-one.R")); invisible(parse("local/build-all.R")); cat("parse ok\n")'` -Expected: `parse ok`. - -- [ ] **Step 4: Commit** - -```bash -git add local/build-one.R local/build-all.R -git commit -m "feat(patches): pass patch registry to bincraft build calls" -``` - ---- - -### Task B5: Document the feature in the README - -**Files:** -- Modify: `README.md` - -**Interfaces:** -- Produces: a short "Patching packages" section explaining the registry and linking the design spec. - -- [ ] **Step 1: Add a README section** - -Add a `## Patching packages` section after the "Build Process" section describing: why patching exists (compiler/OS-specific failures cascading via shared deps like RcppParallel), where the registry lives (`local/patches/registry.json`), the two tiers (env/configure/Makevars overrides vs source diffs), and that bincraft pre-builds patched binaries served to pak. Link `specs/2026-06-30-package-patching-design.md`. - -- [ ] **Step 2: Commit** - -```bash -git add README.md -git commit -m "docs(patches): document the package patching workflow" -``` - ---- - -## Self-Review - -**Spec coverage:** -- Registry in this repo, passed to bincraft → Task A7 (`patches` arg), B1 (registry), B4 (wiring). ✓ -- Mechanism in bincraft (resolve → pre-build → prepend repo → install) → A4–A6. ✓ -- Both tiers, env-first → A4 (`env`, `configure_args`, `makevars`, `patch` all applied; env is the cheap default and the RcppParallel entry uses only env). ✓ -- Pre-built patched binary served from a prepended local repo → A0 proof + A5/A6. ✓ -- Cache keyed by pkg/version/platform/arch/rminor/patchhash → A3 (`patch_cache_key`) + A5 (cache use). ✓ -- S3 upload of patched binaries → **partial**: A5 caches locally only. Cross-machine S3 reuse is deferred (see note below) to keep the first cut shippable; local `/mnt/cache` reuse already removes per-dependent rebuilds within a container. Recorded as out-of-first-cut, not dropped. -- Error handling (diff fails → skip+warn; build fails → skip+warn; no match → skip; ambiguous overlap → validation error) → A4 (`apply_source_patch` FALSE path, build tryCatch), A5 (`resolve_patch_version` NA path), B2 (overlap validation). ✓ -- Observability (log line per applied patch) → A5 (`log_info` with `describe_patch`). DB metadata recording of applied patches is deferred with S3 (same note). -- Testing (registry parse/match, cache key, integration, failure path) → A1–A5 unit tests, A8 e2e, A4 patch-fail test. ✓ - -**Deferred from spec (call out to user):** S3 upload/reuse of patched binaries and recording applied patches in the Postgres build-metadata row. The local `/mnt/cache/patched-binaries` cache already prevents repeated rebuilds within a container; S3 reuse across CI jobs is a follow-up. If you want it in the first cut, add a Task A5b (upload `cached` tarball to a `…/patched/` S3 slot and check there before building) and a metadata column — say so and I'll insert them. - -**Placeholder scan:** No TBD/TODO; every code step shows complete code. README/registry-README prose steps describe exact content to write (acceptable for docs). - -**Type consistency:** `prepare_patched_repo` signature is identical across A5 (definition) and A6 (call site, via defaults). `patch_cache_key(entry, version, platform, arch, r_minor)` argument order matches between A3 and A5. `run_pak_install_with_mutex(..., patched_repo)` matches between A6 definition and the A6 test. `handle_system_dependencies(..., patches)` matches between A7 edit and A7 test. `build_patched_binary(entry, version, dest_dir)` matches A4 and A5. - -**Open risk:** Task A0 gates the whole approach; if it fails, the documented source-serving contingency keeps every later task valid with a localized change in A4. diff --git a/plans/2026-08-07-per-minor-edge-routing.md b/plans/2026-08-07-per-minor-edge-routing.md deleted file mode 100644 index 3c3e320..0000000 --- a/plans/2026-08-07-per-minor-edge-routing.md +++ /dev/null @@ -1,166 +0,0 @@ -# Per-R-minor edge routing implementation plan - -Spec: `specs/2026-08-07-per-minor-edge-routing-design.md` - -**Goal:** let a stock `install.packages()` see the per-minor packages by routing `PACKAGES*` requests to `…/src/contrib//`, where `bincraft` publishes a union index. - -**Architecture:** the union is built in `bincraft`; the edge script only redirects index requests, gated on a `UNION_SLOTS` script variable; the script lives in this repo and is applied by OpenTofu. - -**Tech stack:** Deno / TypeScript (Bunny Edge Scripting, SDK 0.12), OpenTofu with `BunnyWay/bunnynet` 0.17, R (bincraft). - -## Global constraints - -- Redirect only `PACKAGES`, `PACKAGES.gz` and `PACKAGES.rds`; never a tarball, because the union index already carries the correct tarball URL for both classes of package. -- Every redirect carries `Cache-Control: no-store`; redirect targets stay UA-independent. -- `UNION_SLOTS` is empty by default, so deploying the script is a no-op until a slot is backfilled. -- A slot is `/`, e.g. `amd64/alpine324`. -- Verified prerequisites: `PACKAGES*` is served `cdn-cache: BYPASS`, so the script sees every index request; `Deno.env.get()` reads script variables; the SDK local server listens on `127.0.0.1:8080`. - ---- - -## Task 1: Edge script and its test matrix - -**Files:** - -- Create: `edge/rpkgs-router.ts` -- Create: `edge/rpkgs-router.test.ts` -- Modify: `justfile` (add `edge-test`) - -**Produces:** a single-file script deployable as `bunnynet_compute_script.content`, reading `UNION_SLOTS` from the environment. - -- [ ] **Step 1: write the test matrix first** - -`edge/rpkgs-router.test.ts` spawns `deno run -A edge/rpkgs-router.ts` with `UNION_SLOTS=amd64/alpine324`, waits for `127.0.0.1:8080`, and issues requests with `redirect: "manual"`. - -Cases, asserted on the `location` header (or its absence): - -| # | path | User-Agent | expectation | -| --- | ------------------------------------------------------ | --------------------------------------------- | ------------------------------------------------------------ | -| 1 | `/amd64/alpine324/latest/src/contrib/PACKAGES.gz` | `R (4.5.3 x86_64-pc-linux-musl …)` | 302 → `…/src/contrib/4.5/PACKAGES.gz` | -| 2 | same | `R (4.6.0 …)` | 302 → `…/src/contrib/4.6/PACKAGES.gz` | -| 3 | same, but slot `amd64/noble` | `R (4.5.3 …)` | no redirect (slot not in `UNION_SLOTS`) | -| 4 | `…/src/contrib/curl_7.1.0.tar.gz` | `R (4.5.3 …)` | no redirect | -| 5 | `…/src/contrib/4.5/PACKAGES.gz` | `R (4.5.3 …)` | no redirect (loop guard) | -| 6 | `…/src/contrib/PACKAGES.gz` | `curl/8.0` | no redirect (no R minor) | -| 7 | `/src/contrib/PACKAGES.gz` | alpine UA with `Alpine Linux … 3.24` | 302 → `/amd64/alpine324/latest/src/contrib/4.5/PACKAGES.gz` | -| 8 | `/src/contrib/PACKAGES.gz` | `R (4.5.3 x86_64-pc-linux-musl …)`, no distro | 302 → `cran.r-project.org`, **not** a `linux-musl` slot | -| 9 | `/src/contrib/foo_1.0.tar.gz` | `R (4.5.1 aarch64-apple-darwin20 …)` | 302 → `/bin/macosx/big-sur-arm64/contrib/4.5/foo_1.0.tar.gz` | -| 10 | `/bin/macosx/big-sur-arm64/contrib/4.5/foo_1.0.tar.gz` | any | 302 → `cran.r-project.org` | -| 11 | any redirect above | — | `cache-control: no-store` | - -- [ ] **Step 2: run the tests and watch them fail** - -`just edge-test` → every case fails, because `edge/rpkgs-router.ts` does not exist. - -- [ ] **Step 3: write `edge/rpkgs-router.ts`** - -Order of evaluation in `onOriginRequest`: - -1. normalise `//` runs in the path -2. darwin `/src/contrib/*` → `/bin/macosx//contrib//` -3. `/bin/macosx/**` → CRAN -4. `/{arch}/{os}/latest/src/contrib/`: pass through if `rest` already starts with `/`, or is not an index file, or the slot is not in `UNION_SLOTS`, or the UA has no R minor; otherwise redirect into `/` -5. `/`, `/src/contrib`, `/src/contrib/**`: resolve arch+os from the UA, redirect to CRAN when the distro is unidentifiable, otherwise redirect to the qualified path, adding `/` under the same index-file rule -6. anything else: pass through - -The R minor comes from either `R/4.5.3` or `R (4.5.3 …)`, so a stock UA is enough. The `linux-gnu` / `linux-musl` fallback in `parseUserAgent` is deleted: those are not slot names. - -- [ ] **Step 4: run the tests until they pass** - -`just edge-test` - -- [ ] **Step 5: commit** - -```bash -git add edge/rpkgs-router.ts edge/rpkgs-router.test.ts justfile -git commit -m "feat(edge): route PACKAGES requests to the per-R-minor slot" -``` - ---- - -## Task 2: Manage the script from OpenTofu - -**Files:** - -- Modify: `cdn.tf` - -**Consumes:** `edge/rpkgs-router.ts` from Task 1. - -- [ ] **Step 1: add the resources** - -```terraform -resource "bunnynet_compute_script" "rpkgs_router" { - type = "middleware" - name = "rpkgs-router" - content = file("${path.module}/edge/rpkgs-router.ts") -} - -resource "bunnynet_compute_script_variable" "rpkgs_router_union_slots" { - script = bunnynet_compute_script.rpkgs_router.id - name = "UNION_SLOTS" - default_value = "" - required = false -} -``` - -and replace `middleware_script = 29277` with `middleware_script = bunnynet_compute_script.rpkgs_router.id`. - -- [ ] **Step 2: validate** - -`tofu init -backend=false && tofu validate` - -- [ ] **Step 3: import the existing script (needs `BUNNYNET_API_KEY`)** - -```bash -tofu import bunnynet_compute_script.rpkgs_router 29277 -tofu plan -``` - -The plan must show an in-place `content` update and **no** replacement of the pull zone. A replacement means the import did not take. - -- [ ] **Step 4: commit** - -```bash -git add cdn.tf -git commit -m "feat(cdn): manage the edge middleware script from this repo" -``` - ---- - -## Task 3: Union index writer in bincraft - -**Files (repo `codefloe.com/rpkgs/bincraft`):** - -- Modify: `R/package_index.R` -- Test: `tests/testthat/test-package_index.R` - -**Produces:** `write_union_index(flat_records, minor_records)` returning the merged records, called from `upload_package_index()` when `r_minor` is set. - -- [ ] **Step 1: write the failing tests** - -- a package present in both slots keeps the per-minor record, with `Path = "4.5"` -- a package only in the flat slot survives with no `Path` -- a package only in the per-minor slot survives with `Path = "4.5"` -- a union smaller than the flat input raises an error rather than returning - -- [ ] **Step 2: run them and watch them fail** - -`Rscript -e 'testthat::test_file("tests/testthat/test-package_index.R")'` - -- [ ] **Step 3: implement `write_union_index()` and call it from `upload_package_index()`** - -After `update_PACKAGES()` has written the per-minor index, read the flat slot's `PACKAGES.rds`, set `Path = ` on the per-minor records, drop the flat records for packages the per-minor slot already has, and rewrite `PACKAGES`, `PACKAGES.gz` and `PACKAGES.rds` in the per-minor slot. - -- [ ] **Step 4: run the tests until they pass** - -- [ ] **Step 5: commit and open the PR against bincraft** - ---- - -## Task 4: Roll out slot by slot - -- [ ] Re-index one slot (`amd64/alpine324`, R 4.5) and confirm the union index lists both `curl` (per-minor, `Path: 4.5`) and `jsonlite` (flat, no `Path`). -- [ ] Set `UNION_SLOTS = "amd64/alpine324"` and confirm in `reg.devxy.io/r/r-alpine:4.5-3.24` that `available.packages()` returns the union count and `"curl" %in% rownames(...)`. -- [ ] Add `arm64/alpine324`, then the remaining slots. - -`install.packages("curl")` will still fail to build on `alpine324` until that slot's source tarballs are replaced with real binaries. That is tracked separately. diff --git a/provider.tf b/provider.tf index cb5a87d..85b6aa4 100644 --- a/provider.tf +++ b/provider.tf @@ -2,7 +2,7 @@ terraform { required_providers { bunnynet = { source = "registry.terraform.io/BunnyWay/bunnynet" - version = "~> 0.19" + version = "~> 0.20" } } } diff --git a/renovate.json b/renovate.json index 7b2c3b3..3723172 100644 --- a/renovate.json +++ b/renovate.json @@ -1,6 +1,7 @@ { "$schema": "https://docs.renovatebot.com/renovate-schema.json", "extends": ["local>devxy/renovate-config"], + "automergeStrategy": "squash", "ignorePaths": [ "docker/**", ".crow/process-updates.yaml", diff --git a/specs/2026-06-30-package-patching-design.md b/specs/2026-06-30-package-patching-design.md deleted file mode 100644 index 1344173..0000000 --- a/specs/2026-06-30-package-patching-design.md +++ /dev/null @@ -1,157 +0,0 @@ -# Design: Dynamic per-package patching during binary builds - -Date: 2026-06-30 -Status: Approved (pending spec review) - -## Problem - -Some CRAN packages fail to compile on specific build platforms due to compiler- or OS-specific issues that have nothing to do with the package being built. -The canonical example is `RcppParallel`: its bundled Intel TBB sources fail to build on musl (Alpine) and on newer OS/compiler combinations. -Observed failure on `ubuntu-2604` ("resolute") with `g++ 15.2.0`: - -``` -../build/common.inc:74: *** "" is not supported. Add build/.inc file with os-specific settings . Stop. -make: *** [Makevars:163: tbb] Error 2 -ERROR: compilation failed for package 'RcppParallel' -``` - -Because `RcppParallel` is a dependency of many packages, a single such failure cascades: every dependent package (e.g. `rts2`) also fails, even though nothing is wrong with the dependent itself. - -Today there is no way to intervene. -A package can only be **excluded** (`local/excluded-packages.json`), which is all-or-nothing and does not help dependents. - -## Goal - -Allow a curated set of packages to be "patched" — via lightweight build-time overrides or, when necessary, real source diffs — **before** they are installed, whether the package is a direct build target or a transitive dependency pulled in by `pak`. - -## Key constraint that drives the design - -When `RcppParallel` fails here, it is being installed as a **transitive dependency** by `pak`, inside `bincraft::build_binary_package()`. -`pak` downloads, configures, and compiles it in one subprocess; this repo never touches that source. -For a fix to reach a dependency-of-a-dependency, the fixed package must be visible to `pak` itself, where `pak`'s repositories/sources are configured — which is inside `bincraft`. - -Decisions taken during brainstorming: - -- **Mechanism lives in `bincraft`** (the engine), because only there can transitive deps be influenced. -- **Patch tiers: both, env-overrides first.** Support cheap per-package env vars / configure args / Makevars (version-independent) *and* true source diffs (version-pinned), preferring the lightweight override. -- **Registry data lives in this repo** (`build-cran-binaries`) and is passed into `bincraft`, keeping `bincraft` as pure mechanism and the frequently-changing policy data with operational config. - -## Approaches considered - -| Approach | How pak sees the fix | Verdict | -|---|---|---| -| A. Patched **source** repo — drop patched `.tar.gz` source into a local repo, prepend it | pak recompiles from your source | Simple, but env-tier overrides leak globally (one subprocess builds everything) and the dep recompiles on every dependent build | -| **B. Pre-built patched binary repo (chosen)** | pak installs a ready binary by repo priority | Per-package scoping is free; no recompile; the binary is a cacheable/uploadable artifact that fits the existing system | -| C. pkgdepends per-build hook | intercept each build | No clean per-package pre-compile hook exists; fragile | - -Chosen: **B**. - -## Architecture - -### Registry (this repo) - -``` -local/patches/ - registry.json # the manifest - RcppParallel/ - fix.patch # optional source diff, referenced by an entry -``` - -`registry.json` is an array of entries: - -```json -[ - { - "package": "RcppParallel", - "versions": "*", - "platforms": ["alpine", "ubuntu-2604"], - "env": { "RCPP_PARALLEL_USE_TBB": "0" }, - "configure_args": [], - "makevars": {}, - "patch": null, - "reason": "bundled TBB fails to build on musl / newer compilers" - } -] -``` - -Field semantics: - -- `package` (string, required): CRAN package name. -- `versions` (string, required): `"*"` for any, a constraint such as `">=5.1.0"`, or an exact version `"5.1.11-2"`. - Env-tier fixes are typically `"*"`; source diffs are normally exact or lower-bounded because a diff is pinned to the source it was generated against. -- `platforms` (array of strings, required): matched against the running build's platform tokens — distro family (`alpine`, `ubuntu`, `redhat`), codename (`ubuntu-2604`, `alpine-324`), and arch (`amd64`, `arm64`). - An entry matches if any listed token matches any build token. - `["*"]` matches all platforms. -- `env` (object, optional): environment variables exported only for this package's isolated build. -- `configure_args` (array, optional): passed as `--configure-args` to the isolated build. -- `makevars` (object, optional): key/value pairs written into a package-local Makevars for the isolated build. -- `patch` (string or null, optional): path (relative to `local/patches/`) to a unified diff applied to the unpacked CRAN source before building. -- `reason` (string, required): human explanation, surfaced in logs and metadata. - -A fix is any combination of `env`, `configure_args`, `makevars`, and `patch`. -"Env-first" is an authoring guideline (prefer the lightweight override) and an ordering of effort, not a runtime branch — all present fields are applied together for the isolated build. - -### Flow (inside bincraft, around existing pak resolution) - -1. **Resolve** the dependency set (dry-run) to learn the concrete versions `pak` will install. - Reuse bincraft's existing resolution where possible (e.g. a `pkgdepends` proposal: `$resolve()` → inspect resolution → ... → `$solve()` / `$install()` after the local repo is prepended). -2. For each resolved package that matches a registry entry (name + `versions` + `platforms`): obtain a **patched binary** for the exact `version × platform × arch × R-minor`: - - **Cache hit** (local `/mnt/cache/patched-binaries/` or S3): fetch it into the local repo. - - **Cache miss**: download the CRAN **source** for that version, apply the source `patch` (if any) to the unpacked tree, build the binary in isolation with `env` / `configure_args` / `makevars` applied, then place the binary in the local repo and write it to the cache (and S3 if uploading is enabled). -3. **Prepend** the local binary repo (`file://…`) to `pak`'s repo list, and regenerate its `PACKAGES` index. -4. Run the **normal install**. - `pak` resolves the patched binary for the matched package — direct or transitive — because it wins on repo priority for an equal version, and installs it without recompiling. - -### Caching (essential) - -`RcppParallel` is a dependency of dozens of packages; without caching the fix would be rebuilt on every dependent build. -Patched binaries are keyed by: - -``` -_____ -``` - -`patchhash` is a hash of the normalized registry entry plus the referenced diff file contents. -Editing a patch therefore changes the hash and auto-invalidates stale cached binaries. - -- Local cache: `/mnt/cache/patched-binaries/`. -- Optional S3 cache for cross-build reuse: a dedicated `…/patched/` slot under the existing arch/codename structure, mirroring how normal binaries are stored. - -### S3 upload - -Patched binaries **are** uploaded to S3 (in addition to the local cache) so they are reused across CI jobs and machines, not just within one container. -They live in a separate `patched/` slot and are not published into the user-facing `src/contrib` index — they are an internal build accelerator, not a distributed artifact. - -## Error handling - -- **Source diff fails to apply** (CRAN moved past the pinned version): log a clear warning, skip that entry, and proceed. - The package builds unpatched (status quo) and may fail. - The skipped/failed-to-apply patch is surfaced in build metadata. -- **Pre-build of the patched binary fails**: log a warning, skip, proceed. -- **No version or platform match**: skip silently (the entry simply does not apply to this build). -- **Overlapping entries for one package**: the most specific entry wins (a concrete `platforms`/`versions` beats `"*"`). - Genuine ambiguity (two equally specific, conflicting entries) is a validation error reported before the build. - -## Observability - -- One log line per applied patch, e.g.: `Applying patch to RcppParallel 5.1.11-2 [env: RCPP_PARALLEL_USE_TBB=0]: bundled TBB fails on musl / newer compilers` -- The set of applied patches (package, version, `patchhash`) is recorded in the Postgres build-metadata row for the build, so it is queryable later. - -## Testing - -- **Unit (registry):** parsing and matching — version constraints, platform token matching, precedence/specificity, and detection of ambiguous overlaps. -- **Unit (cache key):** `patchhash` changes when the entry or diff changes; is stable otherwise. -- **Integration:** `RcppParallel` on `resolute` (and/or Alpine) fails to build without a registry entry and succeeds with one; a dependent package such as `rts2` succeeds once the dependency is patched. -- **Failure path:** an entry pinned to an old version against a newer CRAN release → graceful skip with a warning, build continues. - -## Out of scope - -- Shipping a default registry inside `bincraft` (registry is repo-local for now; a baseline-in-engine + repo-override model can come later if needed). -- Publishing patched binaries into the public `src/contrib` index. -- Automatic detection of which packages need patches — entries are curated by hand. - -## Split of work - -- **bincraft:** the mechanism — registry ingestion, resolution hook, isolated patched-binary build, caching/upload, local-repo prepend, logging, metadata recording. - A new `patches` argument on `build_binary_package()`. -- **build-cran-binaries (this repo):** the `local/patches/` registry and diffs, passing `patches = "local/patches"` through `build-one.R` / `build-all.R`, and documentation. diff --git a/specs/2026-08-07-per-minor-edge-routing-design.md b/specs/2026-08-07-per-minor-edge-routing-design.md deleted file mode 100644 index b55812c..0000000 --- a/specs/2026-08-07-per-minor-edge-routing-design.md +++ /dev/null @@ -1,174 +0,0 @@ -# Design: Routing clients to per-R-minor binary slots - -Date: 2026-08-07 -Status: Approved (pending spec review) - -## Problem - -`bincraft` routes ABI-"risky" packages to a per-minor slot `…/latest/src/contrib//` and indexes every directory independently (`upload_package_index()` calls `cranlike::update_PACKAGES()` on one prefix at a time). -Nothing unions those indices, and `contrib.url()` only ever yields `/src/contrib`, so no value of `options(repos)` can address a per-minor slot. -Only `uvr` resolves per-minor URLs, which means the per-minor slots are invisible to `install.packages()` by construction. - -Measured on 2026-08-07: - -| slot | flat `src/contrib` | `src/contrib/4.5` | unique packages only in the per-minor slot | -| ----------------- | ------------------ | ----------------- | ------------------------------------------ | -| `amd64/alpine324` | 21 640 | 3 310 | 2 886 | -| `amd64/noble` | 24 495 | 398 | 23 | - -This is what issue #63 records as "missing binaries" on `alpine324`. -The packages are not missing; they are in a directory base R cannot reach. -The user-visible symptom in `reg.devxy.io/r/r-alpine:4.5-3.24` is: - -``` -> install.packages("curl") -Warning message: -package 'curl' is not available for this version of R -``` - -A second, unrelated defect exists on the same slot and is **out of scope here**: many `alpine324` tarballs are byte-identical CRAN _source_ tarballs that the index nevertheless stamps `Built: R 4.5.3; …-linux-musl`. -Routing exposes `curl`; only a rebuild of that slot makes it install. - -## Goal - -Let a stock `install.packages()` see one complete package list for its own R minor, without duplicating tarballs and without an R-version-varying cache key anywhere in the CDN. - -## Key constraint that drives the design - -R resolves a package's download URL from the index, not from the request path, and it keeps the `contriburl` it _asked for_ rather than the one it was redirected to. -Measured with `options(repos = …/latest)` against a middleware that redirects the index into `4.5/`: - -``` -curl available: TRUE -curl repo: …/latest/src/contrib # the flat URL, not the 4.5 one it was served from -``` - -So the union index is always addressed relative to the **flat** directory, whatever path it was fetched from. -`available.packages()` honours a `Path:` field and folds it into the `Repository` column, which gives the whole routing for free: - -- a per-minor record carries `Path: `, so its tarball is fetched from `…/src/contrib//` -- a flat record carries no `Path`, so its tarball is fetched from `…/src/contrib/` - -Verified end to end against the live CDN with a locally built union index for `amd64/alpine324` (31 507 records): - -``` -curl: 7.1.0 -> …/latest/src/contrib/4.5 -> curl_7.1.0.tar.gz 717 725 B -jsonlite: 2.0.0 -> …/latest/src/contrib -> jsonlite_2.0.0.tar.gz 1 055 849 B -``` - -The corollary is that the edge script must **not** rewrite tarball requests: every tarball URL is already correct when it leaves the client, and redirecting one into `/` would break exactly the flat packages the union is meant to preserve. - -The complementary trick does not work: R's `gzcon()` reads only the first member of a concatenated gzip stream (10 291 of an expected 31 931 records), so an edge-side merge would have to fully decompress and recompress both indices and additionally 404 `PACKAGES.rds` to stop R preferring it. -That is why the union is produced in `bincraft`, not at the edge. - -## Approaches considered - -| Approach | Where the union lives | Verdict | -| ----------------------------------------------------------- | ------------------------------------------------------------ | ----------------------------------------------------------------------------------------------------------- | -| **A. Union index written by `bincraft` (chosen)** | per-minor `PACKAGES*`, per-minor entries carry `Path: ` | Edge does one redirect; `PACKAGES.rds` stays correct; no duplication | -| B. Merge at the edge | middleware fetches both indices, recompresses | ~2 MB decompress/recompress per cache fill, cache key must include the R minor, breaks R's `.rds` fast path | -| C. Move the minor up the path (`latest//src/contrib/`) | addressable by `options(repos)` directly | No edge logic at all, but a full layout migration and breaks the published URL contract | - -Chosen: **A**. - -## Architecture - -### bincraft: union index (separate PR) - -After writing a per-minor index, republish it as a union of that slot and the flat slot: - -1. Read the flat slot's `PACKAGES.rds` and the per-minor slot's own records. -2. Set `Path: ` on every per-minor record, so its tarball resolves into the per-minor directory. -3. Drop every flat record whose package is already present in the per-minor slot, so the per-minor build always wins, and leave the survivors without a `Path`. -4. Write the merged `PACKAGES`, `PACKAGES.gz` and `PACKAGES.rds` into `…/src/contrib//`. - -Guard: refuse to publish a union with fewer records than the flat index it was built from. -A truncated union is worse than no union, because it silently removes packages from every client on that minor. - -### Edge script (this repo) - -The script routes `PACKAGES`, `PACKAGES.gz` and `PACKAGES.rds` requests, and nothing else. - -``` -normalize path -parseClient(UA) -> { rMinor, arch, os } # rMinor from "R (4.5.3 …)" or "R/4.5.3" -darwin branches # unchanged -if path is /{arch}/{os}/latest/src/contrib/PACKAGES* - already under // ? pass through # loop guard - rMinor known && slot in UNION_SLOTS ? 302 -> …/src/contrib//PACKAGES* - else pass through # flat slot, today's behaviour -if path is /src/contrib/… # bare root - resolve arch + os; unknown -> 302 to CRAN - then apply the same PACKAGES* rule -else pass through -``` - -Redirects carry `Cache-Control: no-store`. -Every cacheable URL is therefore UA-independent, and no cache key has to vary by R version. - -### Repaired bare-root detection - -The bare `https://cran.rpkgs.com` form is currently broken for every Linux client that uses a stock R user agent. -`ALPINE_REGEX`, `UBUNTU_REGEX` and `RHEL_REGEX` only match a Posit-style user agent the user has to set by hand; stock R never carries the distro, so the script falls through to `extractOs()` and redirects to a slot that does not exist: - -``` -UA: R (4.5.3 x86_64-pc-linux-musl …) -> 302 /amd64/linux-musl/latest/… (404) -UA: R (4.5.3 x86_64-pc-linux-gnu …) -> 302 /amd64/linux-gnu/latest/… (404) -``` - -The fallback to a phantom `linux-musl` / `linux-gnu` slot is removed. -An unidentifiable distro redirects to CRAN, which is the existing behaviour for an unparseable user agent. -The R _minor_ is always present in a stock user agent, so per-minor routing itself does not depend on distro detection. - -### Rollout gate - -`UNION_SLOTS` is a `bunnynet_compute_script_variable` listing the slots whose per-minor index is already a union. -It is empty by default, so deploying the script changes nothing until `bincraft` has backfilled a slot, and a rollback is a variable edit rather than a code deploy. -All slots currently carry `4.4`, `4.5` and `4.6`; a client on any other minor falls through to the flat slot. - -### Deployment from this repo - -The script is a file in the repo, applied by the existing OpenTofu configuration: - -``` -edge/rpkgs-router.ts # the script -edge/rpkgs-router.test.ts # UA x path -> expected Location matrix -cdn.tf # bunnynet_compute_script + _variable -``` - -Provider `BunnyWay/bunnynet` v0.17.0 (already pinned) ships `bunnynet_compute_script` with `content` loadable via `file()`, plus `bunnynet_compute_script_variable`. -`middleware_script = bunnynet_compute_script.rpkgs_router.id` replaces the hard-coded `29277`, after a one-time `tofu import` of the existing script. - -## Error handling - -- Unknown R minor, or a slot not listed in `UNION_SLOTS`: pass through to the flat slot. - The client sees exactly today's behaviour. -- Unparseable distro on the bare-root form: redirect to CRAN. -- A request already under `…/src/contrib//`: pass through, so a redirect can never loop. -- A per-minor slot that does not exist for a listed minor: the client gets the origin's 404. - `UNION_SLOTS` is the operator's assertion that the slot is ready, so this is a configuration error, not a runtime condition to paper over. - -## Testing - -Local, before any apply: `deno run -A edge/rpkgs-router.ts` serves the middleware against the real origin, so `edge/rpkgs-router.test.ts` drives the whole matrix against that local server. - -- User agent matrix: R 4.4 / 4.5 / 4.6 on musl and gnu, both arches, Posit-style and stock forms, plus a darwin UA and a non-R UA. -- Path matrix: `PACKAGES`, `PACKAGES.gz`, `PACKAGES.rds`, a tarball, a path already under `4.5/`, and `/src/contrib/…` on the bare root. -- Assertion is the `Location` header (or its absence), not the body. - -After apply, a smoke test against `cran.rpkgs.com`: - -- `available.packages()` inside `reg.devxy.io/r/r-alpine:4.5-3.24` returns the union count, and `"curl" %in% rownames(...)` is `TRUE`. -- A flat-slot package still downloads from `…/src/contrib/`, and a per-minor package downloads from `…/src/contrib//`. - -## Out of scope - -- `Meta/archive.rds` stays flat-only, so `remotes::install_version()` does not see per-minor archives. -- The `alpine324` source-tarball defect: that slot serves CRAN sources stamped as binaries, and needs a rebuild independent of this work. -- Any change to how `uvr` resolves per-minor URLs; it already addresses the slots directly. - -## Split of work - -1. `bincraft`: union index writer plus its guard, and a re-index of one slot to validate. -2. This repo: `edge/rpkgs-router.ts`, its test matrix, and the `cdn.tf` resources with `UNION_SLOTS` empty. -3. Enable `UNION_SLOTS` slot by slot as `bincraft` backfills them. diff --git a/specs/2026-08-12-shard-weekly-rebuild-design.md b/specs/2026-08-12-shard-weekly-rebuild-design.md deleted file mode 100644 index f4ccbaa..0000000 --- a/specs/2026-08-12-shard-weekly-rebuild-design.md +++ /dev/null @@ -1,167 +0,0 @@ -# Design: Sharding and resuming the weekly rebuild - -Date: 2026-08-12 -Status: Approved (pending spec review) - -## Problem - -`weekly-rebuild-missing` runs one job per `-` and walks that slot's rebuild list serially in a single `R -q -e` invocation (`.crow/weekly-rebuild-missing.yaml:165`). -Until 2026-08-09 that was cheap, because every source fallback was skipped as "already built" and the list was effectively empty. -Since bincraft #105/#106/#107 and build-cran-binaries #159 the gate works, and the lists are now large. - -Share of records whose object is byte-identical to CRAN's source, measured against `cran.r-project.org` MD5s on 2026-08-12: - -| slot | records | source-served | share | -| ------------------ | ------: | ------------: | -----------: | -| `amd64/resolute` | 24 212 | 15 023 | 62.1% | -| `arm64/resolute` | 24 291 | 13 670 | 56.3% | -| `arm64/alpine324` | 24 328 | 9 514 | 39.2% | -| `amd64/alpine324` | 24 343 | 8 917 | 36.7% | -| `arm64/rhel10` | 24 695 | 5 384 | 21.9% | -| `amd64/rhel10` | 24 881 | 4 712 | 19.2% | -| 12 remaining slots | ~24 700 | 850 to 2 130 | 3.5% to 8.7% | - -A single serial job cannot absorb that. -Pipeline 10910 (`weekly_rebuild_missing:alpine-324-amd64`) started on 2026-08-09, ran for roughly two days, reached `[8692/23885] cholera`, and was killed there. - -Two distinct failures follow from that shape. - -**No parallelism.** The work is embarrassingly parallel across packages, but one job does all of it. - -**No resumability, and no clean stopping point.** The loop has no terminating condition other than exhausting the list, so the only way to stop it is a kill. -A restarted run re-reads the same list and walks it from the first entry. -It skips completed packages via `check_s3_root_package()`, but that costs a CRAN version resolution and an S3 `HEAD` per package, thousands of times, before it reaches new work. -Worse, a kill is not a pipeline failure: the `Purge CDN cache` step is guarded by `when: status: [success, failure]` (`.crow/weekly-rebuild-missing.yaml:206-207`), and on 10910 it produced no output at all. -So the ~4 600 binaries that run did publish stayed hidden behind stale edge copies. - -## Goal - -Turn each slot's rebuild into bounded, parallel, restartable units, without introducing state that can disagree with the bucket. - -## Design - -### 1. Shard the matrix three ways - -Each of the 18 `OS`/`ARCH` rows in `.crow/weekly-rebuild-missing.yaml` gains `SPLIT_INTO: 3` and `SPLIT_INDEX: 1|2|3`, giving 54 rows. -This mirrors `.crow/build-all-versions.yaml:57-98`, which already shards its matrix four ways per arch. - -Routing needs no change. -The cron filter `cron: weekly-rebuild-missing-${OS}-${ARCH}` and the manual `evaluate: weekly_rebuild_missing == "${OS}-${ARCH}"` both match all three shards of a slot. -Placement stays on the `rpkgs-${ARCH}` group label, so shards queue against available capacity rather than oversubscribing it. - -### 2. Extract the loop into `local/rebuild-missing.R` - -The build is currently a single ~1 500-character `R -q -e` argument. -Shard arithmetic and resume logic do not belong in a YAML string, and none of it is testable there. -The loop moves to `local/rebuild-missing.R`, invoked as `Rscript local/rebuild-missing.R $SPLIT_INTO $SPLIT_INDEX`, mirroring `local/build-all.R`. -Its body is unchanged in substance: read `/tmp/rebuild_pkgs.txt`, subtract `local/excluded-packages.json`, loop with `tryCatch` around `bincraft::build_binary_package()`. - -The slice is **interleaved**, not contiguous: - -```r -# the list is alphabetical and build cost clusters by name (Rcpp*, Bioc*, -# rstan*), so contiguous thirds would be badly unbalanced -mine <- pkgs[seq(split_index, length(pkgs), by = split_into)] -``` - -`local/build-all.R:64` uses contiguous chunks via `cut()`. -That is fine there because its list is every CRAN package and version, so the chunks average out. -Here the list is a filtered backlog in which expensive families sit adjacent, so interleaving is the better default. -Interleaving also makes each shard's `[i/n]` progress representative of the slot as a whole. - -### 3. Resume by re-deriving state from the bucket - -Before the loop, the shard performs one `s3fs::s3_dir_info()` on `devxy-rpkgs-binaries///latest/src/contrib` and reads the `etag` column. -It fetches CRAN's `PACKAGES` once for the latest version and published `MD5sum` of every package. -A package is still outstanding if and only if the object at `_.tar.gz` has an ETag equal to CRAN's `MD5sum` for that version, which is the definition `check_s3_root_package()` already applies one package at a time. - -```r -# one paginated listing instead of ~2900 sequential HEAD requests per shard -info <- s3fs::s3_dir_info(slot_dir) -etag <- setNames(gsub('^"|"$', "", info$etag), basename(info$uri)) - -key <- sprintf("%s_%s.tar.gz", mine, cran_version[mine]) -# keep a package when no object exists yet, or when the object is still -# byte-identical to CRAN's source; drop it once a real binary is published -mine <- mine[is.na(etag[key]) | etag[key] == cran_md5[key]] -``` - -This is the whole resume mechanism. -There is no progress file, no volume, and no database cursor. -A restarted shard recomputes ground truth and continues where it stopped, and it is correct even when a sibling shard, a `process-updates` cron, or a manual `just rebuild` completed something in the meantime. - -Three properties make this the right source of truth: - -- **It is what the build itself checks.** Any other store can disagree with the bucket; this one cannot. -- **It is agent-independent.** `.crow/weekly-rebuild-missing.yaml` mounts no `volumes:`, unlike `.crow/build-all-versions.yaml:132-133`, so `/mnt/cache` is per-job and cannot carry progress anyway. -- **It costs one listing.** `cranlike`'s `s3` fork already does exactly this call against this bucket at ~24 000 objects, so the approach is proven at the required scale. - -It must read ETags rather than the slot index's `Built` field, which is how `local/packages-to-build.R:104-130` answers the same question. -Under this design the index is not rewritten until the dependent re-index pipeline runs (section 5), so mid-run it cannot reflect the current run's progress. - -Packages that genuinely fail to build re-publish their CRAN source, so they stay outstanding and would be retried on every restart. -That is already handled upstream: `bincraft::filter_packages_with_errors()` (`R/build_binaries.R:1018`, `:1143`) drops anything with `error_occurred = TRUE`, and `store_build_metadata = TRUE` is passed on every call. -No additional poison-pill filter is needed here. - -Only the flat `src/contrib` path is considered. -The rebuild call passes no `is_r_minor_sensitive`, so it defaults to `FALSE` and only ever targets the flat path; the resume filter matches that scope deliberately. - -### 4. Give each shard a wall-clock budget - -`local/rebuild-missing.R` takes a budget, defaulting to 20 hours, and breaks out of the loop once it is exceeded: - -```r -# exit cleanly rather than being killed, so the dependent re-index still runs -if (difftime(Sys.time(), started, units = "hours") > budget_hours) { - cat(sprintf("Budget of %sh reached after %d/%d packages; stopping cleanly\n", budget_hours, i, n)) - break -} -``` - -It exits 0 and reports how much of the slice it covered. -Every run then has a terminating condition, the re-index and purge always fire, and the remainder is picked up by the next run with no bookkeeping, because section 3 recomputes the outstanding set from scratch. - -### 5. Move the re-index and purge into `.crow/weekly-rebuild-reindex.yaml` - -Three shards per slot means three concurrent `upload_package_index()` calls on the same S3 prefix. -`cranlike::update_PACKAGES()` lists the live bucket, so an early lister that uploads last publishes an index missing its siblings' work. -The re-index steps (`.crow/weekly-rebuild-missing.yaml:171-176`) and the purge step (`:187-207`) therefore leave that file entirely. - -The new file carries: - -```yaml -depends_on: - - weekly-rebuild-missing -runs_on: [success, failure] -``` - -`runs_on: [success, failure]` validates as a workflow-level key under `crow lint`, so a failing shard no longer withholds the re-index. -The file uses the same 18-row matrix and the same `when:` gating as `weekly-rebuild-missing`, so it only re-indexes slots that actually ran. -Each row re-indexes the flat slot and every per-minor slot. -`scripts/purge_cdn_zone.sh` runs once on a single row, because all hostnames share pull zone `3857050` and 18 identical zone purges would be waste. - -## Failure behaviour - -| case | today | after | -| -------------------------- | ----------------------------------- | ----------------------------------------------------- | -| one package errors | `tryCatch` logs, loop continues | unchanged | -| a shard fails outright | purge runs, re-index does not | re-index and purge run via `runs_on` | -| a shard exceeds its budget | cannot happen, runs until killed | exits 0, re-index and purge run | -| a shard is killed | nothing runs | still nothing; trigger the re-index pipeline alone | -| a shard restarts | re-walks the list, HEAD per package | one listing, resumes at the first outstanding package | - -The known cost of `depends_on` being file-level rather than row-level: on the weekly cron no slot is re-indexed until the slowest of all 54 jobs finishes. -The 20-hour budget bounds that at roughly one day. - -## Out of scope - -- `build-all-versions` still cannot rebuild source fallbacks, because `local/build-all.R:113-122` drops every version with any `single_builds` row for the platform and arch, which is precisely the source-fallback set. That is a separate change. -- Bunny Perma-Cache eviction. `scripts/purge_cdn_zone.sh` purges the regular edge cache only; see the note in `CLAUDE.md` and issue history. -- The audit that produces the rebuild list is unchanged. - -## Verification - -- `crow lint .crow/` passes for both pipeline files. -- `local/rebuild-missing.R` gets unit coverage in `local/tests/` for the two pure pieces: the interleaved slice (disjoint, covering, deterministic) and the outstanding-set filter (source-served ETag kept, binary ETag dropped, absent object kept). -- A single-slot manual run of `alpine-324-amd64` shard 1 confirms the listing shortcut against the live bucket, and that the reported outstanding count is close to the 8 917 measured above divided by three. -- Restarting that shard mid-run confirms it resumes rather than replaying, by comparing the outstanding count it reports on the second start. diff --git a/stats/README.md b/stats/README.md new file mode 100644 index 0000000..133b4d9 --- /dev/null +++ b/stats/README.md @@ -0,0 +1,64 @@ +# Download statistics + +This directory turns the cran.rpkgs.com CDN access logs into the public download numbers on [rpkgs.com/stats](https://rpkgs.com/stats). +The `download-stats` pipeline runs it every hour. + +## Data flow + +1. `collect.ts` reads the last 70 hours of access logs from the Bunny Logging API v2 and inserts each request into `download_stats.events` in the build metadata database. +2. `classify.ts` derives what each request was (package download, index fetch, redirect, probe, noise) from its path, status and User-Agent. +3. `publish.ts` aggregates the events into `stats/downloads.json` in the binaries bucket and purges its CDN copy, so it is served at . +4. `packages.ts` writes one file per package, `stats/packages/.json`, with its daily series and its versions, distributions, R versions and architectures for each window. The dashboard fetches one only when that package is opened, so the main file does not grow with the number of packages. + +Bunny keeps access logs for three days. +Each hourly run re-reads almost all of that window, and events are keyed by Bunny's request id, so re-reading never double counts and up to two days of failed runs lose nothing. +Every run is recorded in `download_stats.runs`, and the dashboard marks any hour that no run covered. + +## Privacy + +Bunny anonymizes IPs at the edge to /24 (IPv4) or /64 (IPv6) before they reach the logs. +Not even that prefix is stored: each event keeps only `client`, an HMAC (keyed with `STATS_CLIENT_SALT`) of the prefix, the network (ASN) and the client platform from the User-Agent. +The public JSON contains counts only, never a client hash, IP or User-Agent. +Countries seen from fewer than three clients are folded into "other". + +## What counts + +| Metric | Definition | +| ---------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| Download | A distinct (client, package, version) served on one UTC day. Raw tarball requests are reported separately, because one CI job can re-download the same package many times a day. | +| Client | A distinct `client` hash that fetched an index or a package. Several machines behind one /24 count once, and one laptop on two networks counts twice, so this is an estimate. | +| Client-day | A client active on a given day, roughly one install session. | + +The same R session seen with and without renv's User-Agent prefix counts as one client, since `clientPlatform()` keys on the R part of the User-Agent. + +## What is separated or excluded + +- **Cloud and CI networks** are kept but reported separately: Microsoft/GitHub, AWS, Google, Hetzner, OVH and other hosting ASNs, plus any network whose name says hosting, cloud or data centre. +- **Our own CI runners** (`STATS_OWN_NETWORKS`) are excluded from every public number and only counted as excluded requests. +- **Internal redirects** (`minor_routing`, `slot_resolution`, `https`) are not counted, only the request they lead to. +- **Redirects away from us** are counted as their own metrics: `unsupported_r_minor` (demand for R versions we do not build), `archive_fallback` (old versions we have no binary for), `unknown_distro` and `macos`. +- **Probes**: renv and pak request `__api__/status` to check for Posit Package Manager. +- **Noise**: vulnerability scanners, crawlers and anything outside the repository layout. + +## Changing the rules + +Change `classify.ts`, add a test case to `classify.test.ts` and bump `RULESET`. +Then recompute every stored event: + +```sh +bun stats/collect.ts --reclassify +``` + +The raw path, status, User-Agent and network are stored for exactly this reason. +Whether a request came from our own runners is decided at ingest, so changing `STATS_OWN_NETWORKS` only affects new events. + +## Running locally + +The secrets are in OpenBao: the Bunny key at `development/agent-dev/bunny`, the database password at `development/agent-dev/rpkgs-metadata-db` and the salt and own networks at `development/agent-dev/rpkgs-download-stats`. + +```sh +bun test stats +bun stats/collect.ts +# write the JSON locally instead of uploading it +bun stats/publish.ts --out downloads.json +``` diff --git a/stats/classify.test.ts b/stats/classify.test.ts new file mode 100644 index 0000000..09c96f8 --- /dev/null +++ b/stats/classify.test.ts @@ -0,0 +1,139 @@ +import { describe, expect, test } from 'bun:test'; +import { classify, clientPlatform, parseNetworks, toEvent, tool, type LogEntry } from './classify'; + +const R_RHEL = 'R/4.5.3 R (4.5.3 x86_64-pc-linux-gnu x86_64 linux-gnu)'; +const RENV_RHEL = 'renv (1.2.3); R/4.5.3 R (4.5.3 x86_64-pc-linux-gnu x86_64 linux-gnu)'; +const R_ALPINE = 'R/4.6.1 (Alpine Linux 3.24.1) (aarch64-unknown-linux-musl aarch64 linux-musl)'; +const R_43 = 'R (4.3.2 x86_64-pc-linux-gnu x86_64 linux-gnu)'; + +const base = { status: 200, user_agent: R_RHEL, asn: 3303, asn_org: 'Swisscom', own: false }; + +describe('classify', () => { + test('flat tarball is a download with package, version and slot', () => { + const d = classify({ ...base, path: '/amd64/rhel9/latest/src/contrib/data.table_1.17.8.tar.gz' }); + expect(d).toMatchObject({ + kind: 'download', + outcome: 'served', + slot: 'amd64/rhel9', + arch: 'amd64', + os: 'rhel9', + package: 'data.table', + version: '1.17.8', + r_minor: '4.5', + r_version: '4.5.3', + tool: 'R', + network: 'other', + }); + }); + + test('per-minor tarball and index keep the slot', () => { + expect(classify({ ...base, path: '/arm64/alpine324/latest/src/contrib/4.6/Rcpp_1.1.0.tar.gz' })).toMatchObject({ + kind: 'download', + slot: 'arm64/alpine324', + package: 'Rcpp', + }); + expect(classify({ ...base, path: '/amd64/rhel9/latest/src/contrib/4.5/PACKAGES.rds' }).kind).toBe('index'); + }); + + test('index redirect with a known minor is internal minor routing', () => { + const d = classify({ ...base, status: 302, path: '/amd64/rhel9/latest/src/contrib/PACKAGES.rds' }); + expect(d).toMatchObject({ kind: 'index', outcome: 'redirect', redirect_reason: 'minor_routing' }); + }); + + test('redirect for an R minor we do not build is unsupported_r_minor', () => { + const d = classify({ ...base, user_agent: R_43, status: 302, path: '/amd64/noble/latest/src/contrib/PACKAGES' }); + expect(d).toMatchObject({ redirect_reason: 'unsupported_r_minor', r_minor: '4.3' }); + }); + + test('archived version without a binary falls back to CRAN', () => { + const d = classify({ + ...base, + status: 302, + path: '/amd64/rhel9/latest/src/contrib/Archive/renv/renv_1.2.3.tar.gz', + }); + expect(d).toMatchObject({ kind: 'download', redirect_reason: 'archive_fallback', package: 'renv' }); + }); + + test('bare URL: identified distro resolves to a slot, stock R UA goes to CRAN', () => { + expect( + classify({ ...base, user_agent: R_ALPINE, status: 302, path: '/src/contrib/PACKAGES.rds' }).redirect_reason, + ).toBe('slot_resolution'); + expect(classify({ ...base, status: 302, path: '/src/contrib/PACKAGES.rds' }).redirect_reason).toBe( + 'unknown_distro', + ); + }); + + test('renv and pak probing for Posit Package Manager is a probe', () => { + expect(classify({ ...base, status: 404, path: '/amd64/__api__/status' }).kind).toBe('probe'); + }); + + test('scanner paths are noise', () => { + for (const path of ['/.env', '/wp-content/plugins/x.php', '/.git/config', '/xwpg.php']) { + expect(classify({ ...base, status: 404, user_agent: 'Mozilla/5.0', path }).kind).toBe('noise'); + } + }); + + test('the stats file is not counted as repository use', () => { + expect(classify({ ...base, path: '/stats/downloads.json' }).kind).toBe('stats'); + }); + + test('cloud networks are labelled, own networks win over cloud', () => { + expect(classify({ ...base, asn: 8075, asn_org: 'Microsoft Corporation', path: '/' }).network).toBe('cloud'); + expect(classify({ ...base, asn: null, asn_org: 'Some Hosting Ltd', path: '/' }).network).toBe('cloud'); + expect(classify({ ...base, asn: 24940, own: true, path: '/' }).network).toBe('own'); + }); + + test('query strings and doubled slashes do not change the result', () => { + expect(classify({ ...base, path: '/amd64//rhel9/latest/src/contrib/PACKAGES.rds?x=1' }).kind).toBe('index'); + }); +}); + +describe('client identity', () => { + test('renv and plain R from the same machine share one platform key', () => { + expect(clientPlatform(RENV_RHEL)).toBe(clientPlatform(R_RHEL)); + expect(clientPlatform(R_ALPINE)).not.toBe(clientPlatform(R_RHEL)); + }); + + test('tool detection', () => { + expect(tool(RENV_RHEL)).toBe('renv'); + expect(tool(R_RHEL)).toBe('R'); + expect(tool('pak/0.9.0 R/4.5.1 (...)')).toBe('pak'); + expect(tool('curl/8.5.0')).toBe('curl'); + expect(tool('-')).toBe('none'); + expect(tool('Mozilla/5.0 (Windows NT 10.0)')).toBe('browser'); + }); + + test('own networks match by prefix for IPv4 and IPv6', () => { + const own = parseNetworks('203.0.113.0/24, 2001:db8:1:a100::/64'); + const matches = (ip: string) => own.some((m) => m(ip)); + expect(matches('203.0.113.0')).toBe(true); + expect(matches('203.0.114.0')).toBe(false); + expect(matches('2001:db8:1:a100::')).toBe(true); + expect(matches('2001:db8:1:a101::')).toBe(false); + expect(parseNetworks('')).toHaveLength(0); + }); + + test('toEvent hashes the client and never keeps the IP', () => { + const entry: LogEntry = { + timestamp: '2026-11-14T11:45:33.689+00:00', + pullZoneId: 3857050, + requestId: 'abc', + cacheStatus: 'HIT', + statusCode: 200, + bytesSent: 1000, + remoteIp: '203.0.113.0', + countryCode: 'CH', + host: 'cran.rpkgs.com', + path: '/amd64/rhel9/latest/src/contrib/rlang_1.1.6.tar.gz', + userAgent: RENV_RHEL, + asn: 3303, + asnOrganization: 'Swisscom', + }; + const event = toEvent(entry, (key) => `h(${key.length})`, parseNetworks('203.0.113.0/24')); + expect(event).not.toBeNull(); + expect(JSON.stringify(event)).not.toContain('203.0.113'); + expect(event!.own).toBe(true); + expect(event!.network).toBe('own'); + expect(toEvent({ ...entry, requestId: null }, (k) => k, [])).toBeNull(); + }); +}); diff --git a/stats/classify.ts b/stats/classify.ts new file mode 100644 index 0000000..ab09ef6 --- /dev/null +++ b/stats/classify.ts @@ -0,0 +1,410 @@ +/** + * Turns one Bunny access-log entry into a classified download event. + * + * Everything here is pure so the rules can be unit-tested and re-run over the + * stored events (`collect.ts --reclassify`) whenever they change. Bump RULESET + * on any change that alters a derived column, so the public JSON can say which + * rules produced it. + * + * The rules mirror `edge/rpkgs-router.ts`: a redirect is classified by what the + * router would have done with that path and User-Agent, because the Location + * header is not in the logs. + */ + +export const RULESET = 1; + +/** R minors with a per-minor slot; keep in sync with KNOWN_MINORS in cdn.tf. */ +export const KNOWN_MINORS = new Set(['4.4', '4.5', '4.6']); + +export type Kind = + /** A package tarball. */ + | 'download' + /** `PACKAGES*`: one per repository per `install.packages()` call. */ + | 'index' + /** Other repository metadata, e.g. `Meta/archive.rds`. */ + | 'metadata' + /** renv and pak asking whether this is a Posit Package Manager (`__api__/…`). */ + | 'probe' + /** The repository root or `/src/contrib` without a file. */ + | 'root' + /** Our own `stats/` files, so the dashboard does not count itself. */ + | 'stats' + /** Anything else: vulnerability scanners, crawlers, typos. */ + | 'noise'; + +export type Outcome = 'served' | 'redirect' | 'error'; + +export type RedirectReason = + /** Internal hop: index sent to its per-R-minor slot. Not a user-visible event. */ + | 'minor_routing' + /** Internal hop: bare `cran.rpkgs.com` URL resolved to a slot. */ + | 'slot_resolution' + /** Client runs an R minor we do not build for; sent to CRAN for source. */ + | 'unsupported_r_minor' + /** Bare URL from a distro we could not identify; sent to CRAN. */ + | 'unknown_distro' + /** Archived version we have no binary for; sent to the CRAN source tarball. */ + | 'archive_fallback' + /** macOS client; sent to CRAN's macOS binaries. */ + | 'macos' + /** `http://` to `https://`. */ + | 'https' + | 'other'; + +export type Tool = 'R' | 'renv' | 'pak' | 'rig' | 'curl' | 'wget' | 'python' | 'browser' | 'none' | 'other'; + +export type Network = 'cloud' | 'other' | 'own'; + +export interface LogEntry { + timestamp: string; + pullZoneId: number; + requestId: string | null; + cacheStatus: string | null; + statusCode: number; + bytesSent: number; + remoteIp: string | null; + countryCode: string | null; + host: string | null; + path: string | null; + userAgent: string | null; + asn: number | null; + asnOrganization: string | null; +} + +export interface Derived { + ruleset: number; + kind: Kind; + outcome: Outcome; + redirect_reason: RedirectReason | null; + slot: string | null; + arch: string | null; + os: string | null; + r_minor: string | null; + r_version: string | null; + package: string | null; + version: string | null; + tool: Tool; + network: Network; +} + +/** The raw columns kept per event; everything Derived is recomputed from these. */ +export interface RawEvent { + request_id: string; + ts: string; + zone_id: number; + host: string | null; + status: number; + cache_status: string | null; + bytes: number; + path: string; + user_agent: string | null; + country: string | null; + asn: number | null; + asn_org: string | null; + /** HMAC of the (already anonymized) IP prefix, network and client platform. */ + client: string; + /** Matched one of our own networks at ingest. The IP itself is not kept. */ + own: boolean; +} + +export type Event = RawEvent & Derived; + +const SLOT_REGEX = /^\/(amd64|arm64)\/([a-z0-9._-]+)\/latest\/src\/contrib(?:\/(.*))?$/; +const BARE_REGEX = /^\/src\/contrib(?:\/(.*))?$/; +const MACOS_REGEX = /^\/bin\/macosx\/[^/]+\/contrib\/(\d+\.\d+)\/(.+)$/; +const MINOR_DIR_REGEX = /^(\d+\.\d+)\/(.*)$/; +const INDEX_REGEX = /^PACKAGES(\.gz|\.rds|\.json)?$/; +const TARBALL_REGEX = /(?:^|\/)([A-Za-z][A-Za-z0-9.]*)_([0-9][^/_]*)\.(?:tar\.gz|tgz)$/; +const R_VERSION_REGEXES = [/\bR\/(\d+\.\d+\.\d+)/, /\bR \((\d+\.\d+\.\d+)/]; +const ARCH_REGEX = /\b(x86_64|aarch64|arm64)\b/; +/** The distro forms `parseSlot()` in the router recognises. */ +const DISTRO_REGEX = /(almalinux|rocky)[^\d]*\d+|Ubuntu \d+\.\d+|Alpine Linux|alpine-\d/i; + +/** + * Networks whose clients are almost always machines: public clouds, CI + * providers and hosting companies. A request from one of these is reported as + * "cloud / CI" rather than dropped, since a CI pipeline is a real user too, + * just not a person at a laptop. + */ +const CLOUD_ASNS = new Map([ + [8075, 'Microsoft (incl. GitHub Actions runners)'], + [8068, 'Microsoft'], + [8069, 'Microsoft'], + [36459, 'GitHub'], + [16509, 'Amazon'], + [14618, 'Amazon'], + [15169, 'Google'], + [396982, 'Google Cloud'], + [24940, 'Hetzner'], + [213230, 'Hetzner Cloud'], + [212317, 'Hetzner'], + [16276, 'OVH'], + [14061, 'DigitalOcean'], + [51167, 'Contabo'], + [40021, 'Contabo US'], + [63949, 'Akamai / Linode'], + [31898, 'Oracle'], + [45102, 'Alibaba'], + [37963, 'Alibaba'], + [132203, 'Tencent'], + [45090, 'Tencent'], + [13335, 'Cloudflare'], + [62240, 'Clouvider'], + [12876, 'Scaleway'], + [20473, 'Vultr'], + [197540, 'netcup'], + [9009, 'M247'], + [60068, 'Datacamp / CDN77'], + [36352, 'ColoCrossing'], + [47583, 'Hostinger'], + [207990, 'HostRoyale'], +]); + +const CLOUD_ORG_REGEX = /\b(hosting|cloud|server(s)?|data ?cent(er|re)|colo(cation)?|vps|dedicated)\b/i; + +export function isCloudNetwork(asn: number | null, org: string | null): boolean { + if (asn !== null && CLOUD_ASNS.has(asn)) return true; + return org !== null && CLOUD_ORG_REGEX.test(org); +} + +export function normalizePath(path: string): string { + const withoutQuery = path.split('?')[0] ?? ''; + return withoutQuery.replace(/\/{2,}/g, '/'); +} + +export function rVersion(userAgent: string): string | null { + for (const regex of R_VERSION_REGEXES) { + const match = userAgent.match(regex); + if (match) return match[1]; + } + return null; +} + +export function rMinor(version: string | null): string | null { + if (!version) return null; + const [major, minor] = version.split('.'); + return `${major}.${minor}`; +} + +export function tool(userAgent: string): Tool { + const ua = userAgent.trim(); + if (ua === '' || ua === '-') return 'none'; + if (/^renv\b/i.test(ua)) return 'renv'; + if (/\bpak\b/i.test(ua)) return 'pak'; + if (/\brig\b/i.test(ua)) return 'rig'; + if (/^R[ /(]/.test(ua)) return 'R'; + if (/^curl\//i.test(ua)) return 'curl'; + if (/^wget\//i.test(ua)) return 'wget'; + if (/python|aiohttp|httpx/i.test(ua)) return 'python'; + if (/^Mozilla\//.test(ua)) return 'browser'; + return 'other'; +} + +/** + * The part of the User-Agent that identifies the machine rather than the tool. + * + * `renv (1.2.3); R/4.5.3 R (4.5.3 x86_64-pc-linux-gnu …)` and + * `R/4.5.3 R (4.5.3 x86_64-pc-linux-gnu …)` are the same R session, one going + * through renv and one not. Keying distinct clients on the full User-Agent + * would count that machine twice. + */ +export function clientPlatform(userAgent: string): string { + const version = rVersion(userAgent); + if (!version) return userAgent; + const rPart = userAgent.slice(userAgent.search(/\bR[/ ]\(?\d/)); + return rPart.replace(/\bR\/\d+\.\d+\.\d+\s*/, '').trim() || version; +} + +function slotParts(slot: string | null): { arch: string | null; os: string | null } { + if (!slot) return { arch: null, os: null }; + const [arch, os] = slot.split('/'); + return { arch: arch ?? null, os: os ?? null }; +} + +function archFromUserAgent(userAgent: string): string | null { + const match = userAgent.match(ARCH_REGEX); + if (!match) return null; + return match[1] === 'x86_64' ? 'amd64' : 'arm64'; +} + +function fileKind(rest: string): Kind { + if (rest === '') return 'root'; + if (INDEX_REGEX.test(rest)) return 'index'; + if (TARBALL_REGEX.test(rest)) return 'download'; + if (/^Meta\//.test(rest) || /^PACKAGES/.test(rest)) return 'metadata'; + return 'noise'; +} + +function outcome(status: number): Outcome { + if (status === 301 || status === 302 || status === 303 || status === 307 || status === 308) return 'redirect'; + if (status >= 200 && status < 400) return 'served'; + return 'error'; +} + +export function classify( + raw: Pick, + scheme = 'https', +): Derived { + const path = normalizePath(raw.path); + const userAgent = raw.user_agent ?? ''; + const version = rVersion(userAgent); + const minor = rMinor(version); + const result: Derived = { + ruleset: RULESET, + kind: 'noise', + outcome: outcome(raw.status), + redirect_reason: null, + slot: null, + arch: null, + os: null, + r_minor: minor, + r_version: version, + package: null, + version: null, + tool: tool(userAgent), + network: raw.own ? 'own' : isCloudNetwork(raw.asn, raw.asn_org) ? 'cloud' : 'other', + }; + + let rest: string | null = null; + let area: 'slot' | 'bare' | 'macos' | null = null; + + const slot = path.match(SLOT_REGEX); + const bare = path.match(BARE_REGEX); + const macos = path.match(MACOS_REGEX); + + if (path.includes('/__api__/')) { + result.kind = 'probe'; + } else if (path.startsWith('/stats/')) { + result.kind = 'stats'; + } else if (path === '/') { + result.kind = 'root'; + area = 'bare'; + } else if (slot) { + area = 'slot'; + result.slot = `${slot[1]}/${slot[2]}`; + rest = slot[3] ?? ''; + } else if (bare) { + area = 'bare'; + rest = bare[1] ?? ''; + } else if (macos) { + area = 'macos'; + result.os = 'macos'; + rest = macos[2]; + } + + if (rest !== null) { + const minorDir = rest.match(MINOR_DIR_REGEX); + const file = minorDir ? minorDir[2] : rest; + result.kind = fileKind(file); + const tarball = file.match(TARBALL_REGEX); + if (tarball) { + result.package = tarball[1]; + result.version = tarball[2]; + } + } + + const parts = slotParts(result.slot); + result.arch = parts.arch ?? archFromUserAgent(userAgent); + result.os = parts.os ?? result.os; + + if (result.outcome === 'redirect') { + result.redirect_reason = redirectReason(raw.status, area, rest, minor, scheme, userAgent); + } + + return result; +} + +function redirectReason( + status: number, + area: 'slot' | 'bare' | 'macos' | null, + rest: string | null, + minor: string | null, + scheme: string, + userAgent: string, +): RedirectReason { + if (status === 301 || scheme === 'http') return 'https'; + if (area === 'macos' || (area === 'bare' && /darwin/.test(userAgent))) return 'macos'; + if (minor !== null && !KNOWN_MINORS.has(minor) && (area === 'slot' || area === 'bare')) { + return 'unsupported_r_minor'; + } + if (area === 'slot') { + if (rest !== null && rest.startsWith('Archive/')) return 'archive_fallback'; + if (rest !== null && INDEX_REGEX.test(rest)) return 'minor_routing'; + return 'other'; + } + if (area === 'bare') { + // The router resolves a bare URL only when it can identify the distro from + // the User-Agent; otherwise it sends the client to CRAN. A stock R + // User-Agent (`R (4.5.3 x86_64-pc-linux-gnu …)`) names no distro. + return DISTRO_REGEX.test(userAgent) && ARCH_REGEX.test(userAgent) ? 'slot_resolution' : 'unknown_distro'; + } + return 'other'; +} + +/** Parse `"a.b.c.0/24,2001:db8::/64"` into prefix matchers for our own networks. */ +export function parseNetworks(spec: string): ((ip: string) => boolean)[] { + return spec + .split(',') + .map((cidr) => cidr.trim()) + .filter((cidr) => cidr.length > 0) + .map((cidr) => { + const [base, bitsText] = cidr.split('/'); + const v6 = base.includes(':'); + const bits = Number(bitsText ?? (v6 ? 128 : 32)); + const baseBits = ipBits(base); + return (ip: string) => { + if (ip.includes(':') !== v6) return false; + const candidate = ipBits(ip); + return baseBits !== null && candidate !== null && candidate.slice(0, bits) === baseBits.slice(0, bits); + }; + }); +} + +function ipBits(ip: string): string | null { + if (!ip.includes(':')) { + const octets = ip.split('.').map(Number); + if (octets.length !== 4 || octets.some((o) => !Number.isInteger(o) || o < 0 || o > 255)) return null; + return octets.map((o) => o.toString(2).padStart(8, '0')).join(''); + } + const [head, tail] = ip.split('::'); + const headGroups = head ? head.split(':') : []; + const tailGroups = tail ? tail.split(':') : []; + const missing = 8 - headGroups.length - tailGroups.length; + if (ip.includes('::') ? missing < 0 : missing !== 0) return null; + const groups = [...headGroups, ...Array(Math.max(missing, 0)).fill('0'), ...tailGroups]; + return groups + .map((g) => + parseInt(g || '0', 16) + .toString(2) + .padStart(16, '0'), + ) + .join(''); +} + +/** Build the stored event. Returns null for entries without a request id. */ +export function toEvent( + entry: LogEntry, + hashClient: (key: string) => string, + ownNetworks: ((ip: string) => boolean)[], +): Event | null { + if (!entry.requestId) return null; + const ip = entry.remoteIp ?? ''; + const userAgent = entry.userAgent ?? ''; + const raw: RawEvent = { + request_id: entry.requestId, + ts: entry.timestamp, + zone_id: entry.pullZoneId, + host: entry.host, + status: entry.statusCode, + cache_status: entry.cacheStatus, + bytes: entry.bytesSent, + path: entry.path ?? '', + user_agent: entry.userAgent, + country: entry.countryCode, + asn: entry.asn, + asn_org: entry.asnOrganization, + client: hashClient(`${ip}|${entry.asn ?? ''}|${clientPlatform(userAgent)}`), + own: ip !== '' && ownNetworks.some((matches) => matches(ip)), + }; + return { ...raw, ...classify(raw) }; +} diff --git a/stats/collect.ts b/stats/collect.ts new file mode 100644 index 0000000..432c064 --- /dev/null +++ b/stats/collect.ts @@ -0,0 +1,157 @@ +/** + * Pull the CDN access logs into `download_stats.events`. + * + * bun stats/collect.ts # fetch the last 70 hours, insert what is new + * bun stats/collect.ts --reclassify # recompute derived columns after a rule change + * + * Bunny keeps logs for three days, so every run re-reads almost all of that + * window. Events are keyed by Bunny's request id, which makes re-reading free + * of double counts, and a run that fails is simply covered by the next one. + * + * Environment: BUNNYNET_API_KEY, PGPASS, STATS_CLIENT_SALT, and optionally + * STATS_PULL_ZONE (default: cran-rpkgs) and STATS_OWN_NETWORKS (comma-separated + * CIDRs of our own CI runners, kept out of the public numbers). + */ +import { classify, parseNetworks, toEvent, type Event, type LogEntry } from './classify'; +import { connect, migrate } from './db'; + +const PULL_ZONE = Number(process.env.STATS_PULL_ZONE ?? 3857050); +const WINDOW_HOURS = 70; +const PAGE_SIZE = 10000; + +const COLUMNS = [ + 'request_id', + 'ts', + 'zone_id', + 'host', + 'status', + 'cache_status', + 'bytes', + 'path', + 'user_agent', + 'country', + 'asn', + 'asn_org', + 'client', + 'own', + 'ruleset', + 'kind', + 'outcome', + 'redirect_reason', + 'slot', + 'arch', + 'os', + 'r_minor', + 'r_version', + 'package', + 'version', + 'tool', + 'network', +] as const; + +function requireEnv(name: string): string { + const value = process.env[name]; + if (!value) throw new Error(`${name} is not set`); + return value; +} + +async function fetchPage( + apiKey: string, + from: Date, + to: Date, + offset: number, +): Promise<{ data: LogEntry[]; hasMore: boolean }> { + const url = new URL(`https://logging.bunnycdn.com/v2/pullzones/${PULL_ZONE}/logs`); + url.searchParams.set('from', from.toISOString()); + url.searchParams.set('to', to.toISOString()); + url.searchParams.set('order', 'asc'); + url.searchParams.set('limit', String(PAGE_SIZE)); + url.searchParams.set('offset', String(offset)); + + for (let attempt = 1; ; attempt++) { + const response = await fetch(url, { headers: { AccessKey: apiKey, Accept: 'application/json' } }); + if (response.ok) { + const body = (await response.json()) as { data: LogEntry[]; pagination: { hasMore: boolean } }; + return { data: body.data, hasMore: body.pagination.hasMore }; + } + if ((response.status === 429 || response.status >= 500) && attempt < 6) { + await Bun.sleep(2000 * attempt); + continue; + } + throw new Error(`Bunny logging API returned ${response.status}: ${await response.text()}`); + } +} + +async function collect(): Promise { + const apiKey = requireEnv('BUNNYNET_API_KEY'); + const salt = requireEnv('STATS_CLIENT_SALT'); + const ownNetworks = parseNetworks(process.env.STATS_OWN_NETWORKS ?? ''); + const hashClient = (key: string) => new Bun.CryptoHasher('sha256', salt).update(key).digest('hex').slice(0, 20); + + // Fixing `to` up front keeps offset pagination stable while new requests + // keep arriving. + const to = new Date(); + const from = new Date(to.getTime() - WINDOW_HOURS * 3600 * 1000); + + const events: Event[] = []; + let fetched = 0; + for (let offset = 0; ;) { + const page = await fetchPage(apiKey, from, to, offset); + fetched += page.data.length; + for (const entry of page.data) { + const event = toEvent(entry, hashClient, ownNetworks); + if (event) events.push(event); + } + if (!page.hasMore || page.data.length === 0) break; + offset += page.data.length; + } + + const sql = connect(); + await migrate(sql); + let inserted = 0; + for (let i = 0; i < events.length; i += 1000) { + const batch = events.slice(i, i + 1000); + const rows = await sql` + insert into download_stats.events ${sql(batch, ...COLUMNS)} + on conflict (request_id) do nothing + returning 1`; + inserted += rows.length; + } + await sql` + insert into download_stats.runs (zone_id, window_from, window_to, fetched, inserted) + values (${PULL_ZONE}, ${from}, ${to}, ${fetched}, ${inserted})`; + await sql.close(); + + console.log( + `fetched ${fetched} log entries from ${from.toISOString()} to ${to.toISOString()}, inserted ${inserted} new`, + ); +} + +async function reclassify(): Promise { + const sql = connect(); + await migrate(sql); + const rows: Event[] = + await sql`select request_id, path, status, user_agent, asn, asn_org, own from download_stats.events`; + let updated = 0; + for (let i = 0; i < rows.length; i += 1000) { + const batch = rows.slice(i, i + 1000).map((row) => ({ request_id: row.request_id, ...classify(row) })); + await sql.begin(async (tx) => { + for (const row of batch) { + await tx` + update download_stats.events set + ruleset = ${row.ruleset}, kind = ${row.kind}, outcome = ${row.outcome}, + redirect_reason = ${row.redirect_reason}, slot = ${row.slot}, arch = ${row.arch}, os = ${row.os}, + r_minor = ${row.r_minor}, r_version = ${row.r_version}, package = ${row.package}, + version = ${row.version}, tool = ${row.tool}, network = ${row.network} + where request_id = ${row.request_id}`; + } + }); + updated += batch.length; + } + await sql.close(); + console.log(`reclassified ${updated} events`); +} + +if (import.meta.main) { + await (process.argv.includes('--reclassify') ? reclassify() : collect()); +} diff --git a/stats/db.ts b/stats/db.ts new file mode 100644 index 0000000..2169149 --- /dev/null +++ b/stats/db.ts @@ -0,0 +1,75 @@ +import { SQL } from 'bun'; + +/** + * Download events live in their own `download_stats` schema in the build + * metadata database, next to (but never joined with) `single_builds`. + * + * No IP address is stored, not even the /24 prefix Bunny logs: only a salted + * HMAC of it (`client`) and whether it matched one of our own networks. + */ +const SCHEMA = ` +create schema if not exists download_stats; + +create table if not exists download_stats.events ( + request_id text primary key, + ts timestamptz not null, + zone_id integer not null, + host text, + status smallint not null, + cache_status text, + bytes bigint not null, + path text not null, + user_agent text, + country text, + asn integer, + asn_org text, + client text not null, + own boolean not null, + ruleset smallint not null, + kind text not null, + outcome text not null, + redirect_reason text, + slot text, + arch text, + os text, + r_minor text, + r_version text, + package text, + version text, + tool text not null, + network text not null +); + +create index if not exists events_ts_idx on download_stats.events (ts); +create index if not exists events_package_idx on download_stats.events (package) where kind = 'download'; + +-- One row per collection run. The union of successful windows is what the +-- dashboard reports as covered; anything outside it is shown as a gap. +create table if not exists download_stats.runs ( + id bigserial primary key, + zone_id integer not null, + started_at timestamptz not null default now(), + window_from timestamptz not null, + window_to timestamptz not null, + fetched integer not null, + inserted integer not null +); +`; + +export function connect(): SQL { + const password = process.env.PGPASS; + if (!password) throw new Error('PGPASS is not set'); + return new SQL({ + hostname: process.env.PGHOST ?? 'r-binaries.devxy.io', + port: Number(process.env.PGPORT ?? 15432), + database: process.env.PGDATABASE ?? 'build_metadata', + username: process.env.PGUSER ?? 'rpkgs', + password, + tls: true, + max: 2, + }); +} + +export async function migrate(sql: SQL): Promise { + await sql.unsafe(SCHEMA); +} diff --git a/stats/packages.test.ts b/stats/packages.test.ts new file mode 100644 index 0000000..c143bd8 --- /dev/null +++ b/stats/packages.test.ts @@ -0,0 +1,8 @@ +import { expect, test } from 'bun:test'; +import { compareVersions } from './packages'; + +test('versions order numerically, not as text', () => { + expect(['1.10.0', '1.9.2', '1.2-3', '1.2-10'].sort(compareVersions)).toEqual(['1.2-3', '1.2-10', '1.9.2', '1.10.0']); + expect(compareVersions('3.6.6', '3.6.6')).toBe(0); + expect(compareVersions('1.0', '1.0.1')).toBeLessThan(0); +}); diff --git a/stats/packages.ts b/stats/packages.ts new file mode 100644 index 0000000..5f186af --- /dev/null +++ b/stats/packages.ts @@ -0,0 +1,139 @@ +/** + * Per-package statistics, one file per package at `stats/packages/.json`. + * + * Kept out of `downloads.json` so the main file stays small however many + * packages are served: the dashboard only fetches a package's file when that + * package is opened. + * + * The same rules as the repository numbers apply: our own runners are left out, + * and a download is a distinct (client, package, version) on one UTC day. + */ +import type { SQL } from 'bun'; +import type { Breakdown } from './publish'; + +export interface PackageWindow { + downloads: number; + clients: number; + versions: Breakdown[]; + os: Breakdown[]; + rMinor: Breakdown[]; + arch: Breakdown[]; +} + +export interface PackageFile { + schema: 1; + name: string; + generatedAt: string; + /** Highest version seen downloaded, by `numeric_version` ordering. */ + latestVersion: string | null; + /** Position by downloads in the last 30 days, 1-based; null without downloads in that window. */ + rank30d: number | null; + firstDay: string | null; + daily: { date: string; downloads: number; clients: number }[]; + windows: { last7: PackageWindow; last30: PackageWindow; all: PackageWindow }; +} + +const WINDOWS = { last7: 7, last30: 30, all: null } as const; + +type Dimension = 'versions' | 'os' | 'rMinor' | 'arch'; + +export function compareVersions(a: string, b: string): number { + const pa = a.split(/[.-]/).map(Number); + const pb = b.split(/[.-]/).map(Number); + for (let i = 0; i < Math.max(pa.length, pb.length); i++) { + const d = (pa[i] ?? 0) - (pb[i] ?? 0); + if (d !== 0) return d; + } + return 0; +} + +const emptyWindow = (): PackageWindow => ({ downloads: 0, clients: 0, versions: [], os: [], rMinor: [], arch: [] }); + +export async function buildPackageFiles(sql: SQL, generatedAt: string): Promise { + const files = new Map(); + const file = (name: string): PackageFile => { + let f = files.get(name); + if (!f) { + f = { + schema: 1, + name, + generatedAt, + latestVersion: null, + rank30d: null, + firstDay: null, + daily: [], + windows: { last7: emptyWindow(), last30: emptyWindow(), all: emptyWindow() }, + }; + files.set(name, f); + } + return f; + }; + + const daily: { package: string; date: string; downloads: number; clients: number }[] = await sql` + with dl as ( + select distinct client, package, version, os, r_minor, arch, (ts at time zone 'UTC')::date as day + from download_stats.events + where network <> 'own' and outcome = 'served' and kind = 'download' and package is not null + ) + select package, day::text as date, count(distinct (client, version))::int as downloads, + count(distinct client)::int as clients + from dl group by package, day order by package, day`; + for (const row of daily) { + const f = file(row.package); + f.daily.push({ date: row.date, downloads: row.downloads, clients: row.clients }); + f.firstDay ??= row.date; + } + + for (const [windowId, days] of Object.entries(WINDOWS) as [keyof typeof WINDOWS, number | null][]) { + const since = days === null ? new Date(0) : new Date(Date.now() - days * 86400000); + const rows: { + package: string; + dimension: Dimension | null; + key: string | null; + downloads: number; + clients: number; + }[] = await sql` + with dl as ( + select distinct client, package, version, os, r_minor, arch, (ts at time zone 'UTC')::date as day + from download_stats.events + where network <> 'own' and outcome = 'served' and kind = 'download' and package is not null + and ts >= ${since} + ) + select package, + case + when grouping(version) = 0 then 'versions' + when grouping(os) = 0 then 'os' + when grouping(r_minor) = 0 then 'rMinor' + when grouping(arch) = 0 then 'arch' + end as dimension, + coalesce(version, os, r_minor, arch) as key, + count(distinct (client, version, day))::int as downloads, + count(distinct client)::int as clients + from dl + group by package, grouping sets ((), (version), (os), (r_minor), (arch))`; + for (const row of rows) { + const w = file(row.package).windows[windowId]; + if (row.dimension === null) { + w.downloads = row.downloads; + w.clients = row.clients; + } else { + w[row.dimension].push({ key: row.key ?? 'unknown', clients: row.clients, downloads: row.downloads }); + } + } + } + + const ranked = [...files.values()] + .filter((f) => f.windows.last30.downloads > 0) + .sort((a, b) => b.windows.last30.downloads - a.windows.last30.downloads || a.name.localeCompare(b.name)); + ranked.forEach((f, i) => (f.rank30d = i + 1)); + + for (const f of files.values()) { + const versions = f.windows.all.versions.map((v) => v.key).filter((v) => v !== 'unknown'); + f.latestVersion = versions.sort(compareVersions).at(-1) ?? null; + for (const w of Object.values(f.windows)) { + w.versions.sort((a, b) => compareVersions(b.key, a.key)); + for (const dim of ['os', 'rMinor', 'arch'] as const) w[dim].sort((a, b) => b.clients - a.clients); + } + } + return [...files.values()]; +} diff --git a/stats/publish.ts b/stats/publish.ts new file mode 100644 index 0000000..e0f21e3 --- /dev/null +++ b/stats/publish.ts @@ -0,0 +1,392 @@ +/** + * Aggregate `download_stats.events` into the public `stats/downloads.json`. + * + * bun stats/publish.ts --out downloads.json # write locally only + * bun stats/publish.ts # upload to the bucket and purge the CDN copy + * + * The output holds counts only. Distinct clients are counted from the stored + * HMAC, which never leaves the database, and countries seen from fewer than + * MIN_CLIENTS clients are folded into "other" so no single client can be + * picked out by where it is. + * + * Every number excludes our own CI runners. Cloud and CI networks are kept but + * reported separately, since one pipeline can outweigh every person combined. + */ +import { S3Client, type SQL } from 'bun'; +import { KNOWN_MINORS, RULESET } from './classify'; +import { connect, migrate } from './db'; +import { buildPackageFiles, type PackageFile } from './packages'; + +const MIN_CLIENTS = 3; +const PUBLIC_URL = 'https://cran.rpkgs.com/stats/downloads.json'; +const OBJECT_KEY = 'stats/downloads.json'; +const PACKAGES_PREFIX = 'stats/packages/'; + +/** Split of a count by network class. */ +export interface Split { + total: number; + cloud: number; + other: number; +} + +export interface DayStats { + date: string; + /** Hours of this UTC day for which logs were collected (24 = complete). */ + coveredHours: number; + /** Tarball requests served, before any de-duplication. */ + requests: number; + /** Distinct (client, package, version) served on the day. */ + downloads: Split; + /** Distinct clients that fetched an index or a package. */ + clients: Split; + /** Distinct clients sent to CRAN because we do not build for their R minor. */ + unsupportedMinorClients: number; + bytes: number; +} + +export interface WindowStats { + days: number; + downloads: Split; + /** Distinct clients across the whole window. */ + clients: Split; + /** Sum of daily distinct clients: roughly, install sessions. */ + clientDays: Split; + /** Distinct packages with at least one download. */ + packages: number; + /** Bytes served for packages and indexes. */ + bytes: number; +} + +export interface Breakdowns { + network: Breakdown[]; + os: Breakdown[]; + arch: Breakdown[]; + rMinor: Breakdown[]; + tool: Breakdown[]; + country: Breakdown[]; +} + +export interface Breakdown { + key: string; + clients: number; + downloads: number; +} + +export interface PackageStats { + name: string; + downloads7d: number; + clients7d: number; + downloads30d: number; + clients30d: number; + downloadsAll: number; + clientsAll: number; +} + +export interface PublicStats { + schema: 1; + ruleset: number; + generatedAt: string; + firstDay: string | null; + lastDay: string | null; + supportedMinors: string[]; + daily: DayStats[]; + windows: { last7: WindowStats; last30: WindowStats; all: WindowStats }; + /** Distribution of distinct packages fetched per client per day (30 days). */ + packagesPerClientDay: { median: number; p90: number }; + /** Last 30 days. */ + breakdowns: Breakdowns; + /** The same breakdowns for every window; `breakdowns` is `last30`, kept for older readers. */ + breakdownsByWindow: { last7: Breakdowns; last30: Breakdowns; all: Breakdowns }; + /** Last 30 days: clients we sent elsewhere, and why. */ + redirects: { reason: string; clients: number; requests: number }[]; + unsupportedMinors: Breakdown[]; + /** Last 30 days: requests that were not repository use at all. */ + excluded: { ownRunners: number; probes: number; noise: number; errors: number }; + packages: PackageStats[]; +} + +const split = (row: Record, prefix: string): Split => ({ + total: Number(row[`${prefix}_total`] ?? 0), + cloud: Number(row[`${prefix}_cloud`] ?? 0), + other: Number(row[`${prefix}_other`] ?? 0), +}); + +async function coverage(sql: SQL): Promise> { + const runs: { window_from: Date; window_to: Date }[] = + await sql`select window_from, window_to from download_stats.runs order by window_from`; + const merged: [number, number][] = []; + for (const run of runs) { + const from = run.window_from.getTime(); + const to = run.window_to.getTime(); + const last = merged.at(-1); + if (last && from <= last[1]) last[1] = Math.max(last[1], to); + else merged.push([from, to]); + } + const hours = new Map(); + const DAY = 86400000; + for (const [from, to] of merged) { + for (let day = Math.floor(from / DAY) * DAY; day < to; day += DAY) { + const overlap = Math.min(to, day + DAY) - Math.max(from, day); + const key = new Date(day).toISOString().slice(0, 10); + hours.set(key, (hours.get(key) ?? 0) + overlap / 3600000); + } + } + return hours; +} + +async function windowStats(sql: SQL, days: number | null): Promise { + const since = days === null ? new Date(0) : new Date(Date.now() - days * 86400000); + const [row] = await sql` + with ev as ( + select client, network, kind, package, version, (ts at time zone 'UTC')::date as day + from download_stats.events + where network <> 'own' and outcome = 'served' and kind in ('download', 'index') and ts >= ${since} + ), + dl as (select distinct client, network, package, version, day from ev where kind = 'download'), + cd as (select distinct client, network, day from ev) + select + (select count(*) from dl) as downloads_total, + (select count(*) from dl where network = 'cloud') as downloads_cloud, + (select count(*) from dl where network = 'other') as downloads_other, + (select count(distinct client) from ev) as clients_total, + (select count(distinct client) from ev where network = 'cloud') as clients_cloud, + (select count(distinct client) from ev where network = 'other') as clients_other, + (select count(*) from cd) as client_days_total, + (select count(*) from cd where network = 'cloud') as client_days_cloud, + (select count(*) from cd where network = 'other') as client_days_other, + (select count(distinct package) from dl) as packages, + (select coalesce(sum(bytes), 0) from download_stats.events + where network <> 'own' and outcome = 'served' and kind in ('download', 'index') and ts >= ${since}) as bytes, + (select min(day) from ev) as first_day`; + const firstDay = row.first_day ? new Date(row.first_day) : null; + const span = firstDay ? Math.max(1, Math.ceil((Date.now() - firstDay.getTime()) / 86400000)) : 0; + return { + days: days === null ? span : Math.min(days, span), + downloads: split(row, 'downloads'), + clients: split(row, 'clients'), + clientDays: split(row, 'client_days'), + packages: Number(row.packages), + bytes: Number(row.bytes), + }; +} + +async function breakdown(sql: SQL, column: string, since: Date): Promise { + const rows: { key: string | null; clients: number; downloads: number }[] = await sql` + with ev as ( + select ${sql(column)}::text as key, client, kind, package, version, (ts at time zone 'UTC')::date as day + from download_stats.events + where network <> 'own' and outcome = 'served' and kind in ('download', 'index') and ts >= ${since} + ) + select key, count(distinct client)::int as clients, + (select count(*) from (select distinct client, package, version, day from ev e2 + where e2.kind = 'download' and e2.key is not distinct from ev.key) d)::int as downloads + from ev group by key order by clients desc`; + return rows.map((row) => ({ key: row.key ?? 'unknown', clients: row.clients, downloads: row.downloads })); +} + +/** Fold rows below the client threshold into one "other" row. */ +function suppress(rows: Breakdown[]): Breakdown[] { + const kept = rows.filter((row) => row.clients >= MIN_CLIENTS); + const folded = rows.filter((row) => row.clients < MIN_CLIENTS); + if (folded.length === 0) return kept; + return [ + ...kept, + { + key: 'other', + clients: folded.reduce((sum, row) => sum + row.clients, 0), + downloads: folded.reduce((sum, row) => sum + row.downloads, 0), + }, + ]; +} + +async function breakdowns(sql: SQL, since: Date): Promise { + return { + network: await breakdown(sql, 'network', since), + os: await breakdown(sql, 'os', since), + arch: await breakdown(sql, 'arch', since), + rMinor: await breakdown(sql, 'r_minor', since), + tool: await breakdown(sql, 'tool', since), + country: suppress(await breakdown(sql, 'country', since)), + }; +} + +export async function buildStats(sql: SQL): Promise { + const since30 = new Date(Date.now() - 30 * 86400000); + const byWindow = { + last7: await breakdowns(sql, new Date(Date.now() - 7 * 86400000)), + last30: await breakdowns(sql, since30), + all: await breakdowns(sql, new Date(0)), + }; + const covered = await coverage(sql); + + const dailyRows: Record[] = await sql` + with ev as ( + select *, (ts at time zone 'UTC')::date as day from download_stats.events where network <> 'own' + ), + served as (select * from ev where outcome = 'served' and kind in ('download', 'index')), + dl as (select distinct client, network, package, version, day from served where kind = 'download'), + cl as (select distinct client, network, day from served) + select d.day::text as date, + (select count(*) from served s where s.day = d.day and s.kind = 'download')::int as requests, + (select count(*) from dl where dl.day = d.day)::int as downloads_total, + (select count(*) from dl where dl.day = d.day and network = 'cloud')::int as downloads_cloud, + (select count(*) from dl where dl.day = d.day and network = 'other')::int as downloads_other, + (select count(*) from cl where cl.day = d.day)::int as clients_total, + (select count(*) from cl where cl.day = d.day and network = 'cloud')::int as clients_cloud, + (select count(*) from cl where cl.day = d.day and network = 'other')::int as clients_other, + (select count(distinct client) from ev e where e.day = d.day and e.redirect_reason = 'unsupported_r_minor')::int + as unsupported, + (select coalesce(sum(bytes), 0) from served s where s.day = d.day)::bigint as bytes + from (select distinct day from ev) d order by d.day`; + + const daily: DayStats[] = dailyRows.map((row) => ({ + date: String(row.date), + coveredHours: Math.round(Math.min(24, covered.get(String(row.date)) ?? 0) * 10) / 10, + requests: Number(row.requests), + downloads: split(row, 'downloads'), + clients: split(row, 'clients'), + unsupportedMinorClients: Number(row.unsupported), + bytes: Number(row.bytes), + })); + + const [perSession] = await sql` + with per as ( + select client, (ts at time zone 'UTC')::date as day, count(distinct (package, version)) as n + from download_stats.events + where network <> 'own' and outcome = 'served' and kind = 'download' and ts >= ${since30} + group by 1, 2 + ) + select coalesce(percentile_cont(0.5) within group (order by n), 0) as median, + coalesce(percentile_cont(0.9) within group (order by n), 0) as p90 + from per`; + + const redirects: { reason: string; clients: number; requests: number }[] = await sql` + select redirect_reason as reason, count(distinct client)::int as clients, count(*)::int as requests + from download_stats.events + where network <> 'own' and outcome = 'redirect' and ts >= ${since30} + and redirect_reason not in ('minor_routing', 'slot_resolution', 'https') + group by 1 order by clients desc`; + + const unsupportedMinors: Breakdown[] = await sql` + select r_minor as key, count(distinct client)::int as clients, count(*)::int as downloads + from download_stats.events + where network <> 'own' and redirect_reason = 'unsupported_r_minor' and ts >= ${since30} + group by 1 order by clients desc`; + + const [excluded] = await sql` + select + count(*) filter (where network = 'own')::int as own, + count(*) filter (where network <> 'own' and kind = 'probe')::int as probes, + count(*) filter (where network <> 'own' and kind = 'noise')::int as noise, + count(*) filter (where network <> 'own' and kind in ('download', 'index') and outcome = 'error')::int as errors + from download_stats.events where ts >= ${since30}`; + + const packages: PackageStats[] = ( + await sql` + with dl as ( + select distinct client, package, version, (ts at time zone 'UTC')::date as day, ts >= ${since30} as recent, + ts >= ${new Date(Date.now() - 7 * 86400000)} as week + from download_stats.events + where network <> 'own' and outcome = 'served' and kind = 'download' and package is not null + ) + select package as name, + count(distinct (client, version, day)) filter (where week)::int as downloads7d, + count(distinct client) filter (where week)::int as clients7d, + count(distinct (client, version, day)) filter (where recent)::int as downloads30d, + count(distinct client) filter (where recent)::int as clients30d, + count(distinct (client, version, day))::int as downloadsall, + count(distinct client)::int as clientsall + from dl group by package order by downloads30d desc, downloadsall desc, name` + ).map((row: Record) => ({ + name: String(row.name), + downloads7d: Number(row.downloads7d), + clients7d: Number(row.clients7d), + downloads30d: Number(row.downloads30d), + clients30d: Number(row.clients30d), + downloadsAll: Number(row.downloadsall), + clientsAll: Number(row.clientsall), + })); + + return { + schema: 1, + ruleset: RULESET, + generatedAt: new Date().toISOString(), + firstDay: daily[0]?.date ?? null, + lastDay: daily.at(-1)?.date ?? null, + supportedMinors: [...KNOWN_MINORS], + daily, + windows: { + last7: await windowStats(sql, 7), + last30: await windowStats(sql, 30), + all: await windowStats(sql, null), + }, + packagesPerClientDay: { median: Number(perSession.median), p90: Number(perSession.p90) }, + breakdowns: byWindow.last30, + breakdownsByWindow: byWindow, + redirects, + unsupportedMinors, + excluded: { + ownRunners: Number(excluded.own), + probes: Number(excluded.probes), + noise: Number(excluded.noise), + errors: Number(excluded.errors), + }, + packages, + }; +} + +async function purge(url: string): Promise { + const target = new URL('https://api.bunny.net/purge'); + target.searchParams.set('url', url); + target.searchParams.set('async', 'false'); + const response = await fetch(target, { method: 'POST', headers: { AccessKey: process.env.BUNNYNET_API_KEY ?? '' } }); + if (!response.ok) throw new Error(`purging ${url} failed with ${response.status}: ${await response.text()}`); +} + +async function upload(body: string, packages: PackageFile[]): Promise { + const s3 = new S3Client({ + accessKeyId: process.env.B2_S3_ACCESS_KEY, + secretAccessKey: process.env.B2_S3_SECRET_KEY, + bucket: process.env.STATS_BUCKET ?? 'devxy-rpkgs-binaries', + endpoint: 'https://s3.eu-central-003.backblazeb2.com', + region: 'eu-central-003', + }); + // Package files first, so the main file never lists a package whose own + // file is not there yet. + for (let i = 0; i < packages.length; i += 10) { + await Promise.all( + packages + .slice(i, i + 10) + .map((p) => s3.write(`${PACKAGES_PREFIX}${p.name}.json`, JSON.stringify(p), { type: 'application/json' })), + ); + } + await s3.write(OBJECT_KEY, body, { type: 'application/json' }); + + // The pull zone overrides origin caching with a one-year TTL, so the edge + // copies have to be purged for the new files to show up. + await purge(`https://cran.rpkgs.com/${PACKAGES_PREFIX}*`); + await purge(PUBLIC_URL); +} + +if (import.meta.main) { + const sql = connect(); + await migrate(sql); + const stats = await buildStats(sql); + const packages = await buildPackageFiles(sql, stats.generatedAt); + await sql.close(); + const body = JSON.stringify(stats); + + const outIndex = process.argv.indexOf('--out'); + if (outIndex !== -1) { + const out = process.argv[outIndex + 1]; + await Bun.write(out, JSON.stringify(stats, null, 2)); + const dir = `${out.replace(/[^/]*$/, '')}packages`; + for (const p of packages) await Bun.write(`${dir}/${p.name}.json`, JSON.stringify(p, null, 2)); + console.log(`wrote ${out} (${body.length} bytes) and ${packages.length} files in ${dir}`); + } else { + await upload(body, packages); + console.log( + `published ${PUBLIC_URL} (${body.length} bytes, ${stats.daily.length} days, ${stats.packages.length} packages)`, + ); + } +}