Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/interplane-forge/blob/main/research/sync.sh # interplane-forge INTERPLANE Forge Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `interplane-forge / research / sync.sh` 485 lines ยท 23281 bytes Raw Blame History 1 #!/bin/sh 2 # Network and filesystem IO only; the pinned Omega filters admit metadata, requests and text. 3 # No token is read. Nothing here runs in forged. See research/README.md for install and paths. 4 set -eu 5 umask 077 6 export LC_ALL=C 7 HERE=$(CDPATH= cd -- "$(dirname "$0")/.." && pwd) 8 BIN=${RESEARCH_TOOLS:?set RESEARCH_TOOLS to the folder built by research/build-tools.sh} 9 SYNC=$BIN/research-sync 10 FLAT=$BIN/research-jflat 11 W=$(mktemp -d) 12 # pending names a registry snapshot created by activate but not yet committed by the pointer swap. 13 pending= 14 linkpending= 15 cleanup() { 16 if [ -n "$pending" ]; then rm -rf "$pending"; fi 17 [ -z "$linkpending" ] || rm -f "$linkpending" 18 rm -rf "$W" 19 } 20 trap cleanup EXIT 21 trap 'exit 1' HUP INT TERM 22 refuse() { echo "research sync: $*" >&2; exit 1; } 23 TAB=$(printf '\t') 24 # -q is first so no .curlrc (CURL_HOME, XDG, HOME) can add options; --globoff keeps [] and {} literal; 25 # --proxy '' ignores proxy environment; https only; the URL is passed as data to --url, never as a 26 # bare argument curl could read as an option. 27 zcurl() { 28 curl -q --globoff --proxy '' --proto '=https' --max-time 60 --max-filesize `8388607` -fsS \ 29 -A 'interplane-research/1' "$@" 30 } 31 fetch() { 32 # Fixed public Zenodo host only, no redirect, no auth, no user-chosen host or scheme. 33 case $1 in https://zenodo.org/*) ;; *) echo 'research sync: not a public Zenodo URL' >&2; return 1;; esac 34 zcurl --url "$1" -o "$2" 35 } 36 flatten() { 37 # Decoded paths can expand beyond raw JSON size. Bound the pipe before writing a temp file. 38 { rc=0; "$FLAT" < "$1" || rc=$?; printf '%s\n' "$rc" > "$W/flat-status"; } | head -c `8388608` > "$2" 39 [ "$(cat "$W/flat-status")" = 0 ] || return 1 40 [ "$(wc -c < "$2")" -lt `8388608` ] || { echo 'research sync: decoded metadata exceeds boundary' >&2; return 1; } 41 } 42 blobread() { 43 # Bounded transport copy, analogous to curl --max-filesize. The git object is pinned. 44 { rc=0; timeout 30 env -i PATH=/usr/bin:/bin /usr/bin/git --git-dir="$repo" show "$commit:$1" || rc=$?; printf '%s\n' "$rc" > "$W/blob-status"; } | head -c `8388608` > "$2" 45 [ "$(cat "$W/blob-status")" = 0 ] || return 1 46 [ "$(wc -c < "$2")" -lt `8388608` ] || { echo 'research sync: repository blob exceeds metadata/file boundary' >&2; return 1; } 47 } 48 blobhash() { 49 { rc=0; timeout 30 env -i PATH=/usr/bin:/bin /usr/bin/git --git-dir="$repo" show "$commit:$1" || rc=$?; printf '%s\n' "$rc" > "$W/hash-status"; } | sha256sum > "$W/hash" 50 [ "$(cat "$W/hash-status")" = 0 ] || return 1 51 cut -d ' ' -f1 "$W/hash" 52 } 53 # textcopy SLUG SOURCE STAGE [FIXTURE_DIR]: STAGE/SLUG.txt, or nothing. Every copy owns a fresh 54 # scratch folder, so the decoder never meets an earlier output and keeps its no-overwrite rule. 55 textcopy() ( 56 slug=$1; source=$2; dest=$3; fixture=${4:-} 57 rm -f "$dest/$slug.txt" 58 scratch=$(mktemp -d "$W/copy.XXXXXXXX") || return 1 59 if [ -n "$fixture" ]; then 60 if [ ! -f "$fixture/$slug" ]; then rm -rf "$scratch"; return 0; fi 61 cp "$fixture/$slug" "$scratch/paper" || { rm -rf "$scratch"; return 1; } 62 else 63 if ! fetch "$source" "$scratch/paper"; then 64 echo "research sync: no full text for $slug" >&2 65 rm -rf "$scratch" 66 return 0 67 fi 68 fi 69 # PDF extraction is an installed read-only host dependency, not a repository command. 70 if [ "$(dd if="$scratch/paper" bs=5 count=1 2>/dev/null)" = '%PDF-' ]; then 71 if ! sh "$HERE/research/decode.sh" "$scratch/paper" "$scratch/plain"; then 72 echo "research sync: decoder refused $slug; metadata only" >&2 73 rm -rf "$scratch" 74 return 0 75 fi 76 else 77 mv "$scratch/paper" "$scratch/plain" || { rm -rf "$scratch"; return 1; } 78 fi 79 if ! "$SYNC" text < "$scratch/plain" > "$scratch/text"; then rm -rf "$scratch"; return 0; fi 80 mkdir -p "$dest" && mv "$scratch/text" "$dest/$slug.txt" || { rm -rf "$scratch"; return 1; } 81 rm -rf "$scratch" 82 ) 83 # purge_legacy CATALOG TEXTDIR: an unhashed SLUG.txt cannot say which bundle it belongs to. 84 # Remove it for every refreshed work, whether or not new text was copied. 85 purge_legacy() { 86 grep "^W$TAB" "$1" | cut -f2 > "$W/refreshed-ids" || : 87 while IFS= read -r slug; do rm -f "$2/$slug.txt" || return 1; done < "$W/refreshed-ids" 88 } 89 # bindtext CATALOG SERIAL STAGE TEXTDIR: Omega binds EVERY W bundle of the fresh raw catalog to 90 # R (slug, import serial) and T (slug, sha256 of STAGE/slug.txt, or '-'). Only text copied into 91 # this import's private STAGE can be bound. The plan names each fresh text; it is installed 92 # under its immutable content name TEXTDIR/slug.sha.txt and the catalog is replaced. 93 bindtext() { 94 "$SYNC" bind "$2" "$3/" < "$1" > "$1.bound" 3> "$1.plan" || return 1 95 mkdir -p "$4" || return 1 96 while IFS="$TAB" read -r slug sha; do 97 case $sha in *[!0-9a-f]*|'') return 1;; esac 98 [ "${#sha}" = 64 ] || return 1 99 [ "$(sha256sum < "$3/$slug.txt" | cut -d ' ' -f1)" = "$sha" ] || return 1 100 mv "$3/$slug.txt" "$3/$slug.$sha.txt" || return 1 101 mv -f "$3/$slug.$sha.txt" "$4/$slug.$sha.txt" || return 1 102 done < "$1.plan" 103 purge_legacy "$1.bound" "$4" || return 1 104 mv "$1.bound" "$1" 105 } 106 # Direct import: identity and origin fields are checked by Omega, not this transport. 107 # --zenodo ORCID DID DATE AUTHOR OUT [RESPONSE_JSON [FIXTURE_TEXT_DIR [SERIAL]]] 108 if [ "${1:-}" = --zenodo ]; then 109 shift 110 [ "$#" -ge 5 ] && [ "$#" -le 8 ] || refuse 'usage: --zenodo ORCID DID DATE AUTHOR OUT [RESPONSE_JSON [TEXT_DIR [SERIAL]]]' 111 orcid=$1; did=$2; date=$3; author=$4; out=$5; response=${6:-}; texts=${7:-}; seq=${8:-1} 112 if [ -n "$response" ]; then 113 flatten "$response" "$W/flat" 114 else 115 # Percent-encode the quoted query through curl. It handles transport only. 116 page=1; : > "$W/flat" 117 while :; do 118 zcurl -G --url https://zenodo.org/api/records --data-urlencode "q=creators.orcid:\"$orcid\"" --data-urlencode size=25 --data-urlencode "page=$page" -o "$W/response" 119 flatten "$W/response" "$W/page" 120 # Empty search hit arrays produce /hits/hits []. This is an IO loop terminator. 121 grep -q '^/hits/hits/[0-9]' "$W/page" || break 122 cat "$W/page" >> "$W/flat" 123 [ "$(wc -c < "$W/flat")" -lt `8388608` ] || refuse 'decoded metadata exceeds boundary' 124 records=$(grep -c '^/hits/hits/[0-9][0-9]*/id[[:space:]]' "$W/flat" || :) 125 [ "$records" -le 200 ] || refuse 'more than 200 records for one author' 126 page=$((page + 1)) 127 done 128 fi 129 "$SYNC" zenodo "$orcid" "$did" "$date" "$author" < "$W/flat" > "$W/registry" 3> "$W/texts" 130 [ -s "$W/registry" ] || refuse 'no record carried the claimed ORCID iD' 131 # Stage registry and licensed text separately: a failed download leaves metadata, not an invented text. 132 mkdir -p "$(dirname "$out")" 133 dest=$(dirname "$out")/../../text 134 stage=$W/fresh 135 mkdir -p "$stage" 136 if [ -z "$response" ] || [ -n "$texts" ]; then 137 while IFS="$TAB" read -r slug source; do 138 [ -n "$slug" ] || continue 139 textcopy "$slug" "$source" "$stage" "$texts" 140 done < "$W/texts" 141 fi 142 # Every refreshed work is rebound, including works with no eligible or successful text. 143 bindtext "$W/registry" "$seq" "$stage" "$dest" || refuse 'cannot bind refreshed works to their text' 144 mv "$W/registry" "$out" 145 exit 0 146 fi 147 # --queue ADDDIR/ STATE SHELF_ALLOW SHELF_REMOVE REGISTRY/ SHELF_LIST RADICLE_STORAGE 148 [ "${1:-}" = --queue ] && [ "$#" -eq 8 ] || refuse 'usage: --queue ADDDIR/ STATE ALLOW REMOVE REGISTRY/ SHELFLIST STORAGE' 149 add=$2; state=$3; allow=$4; remove=$5; registry=$6; shelf=$7; storage=$8 150 mkdir -p "$registry" "$(dirname "$state")" 151 registry=$(CDPATH= cd -- "$registry" && pwd -P) 152 # One writer; publication takes the same lock before choosing a complete immutable generation. 153 exec 8>"$registry/.sync.lock" 154 flock 8 155 mkdir -p "$registry/.generations" 156 old= 157 if [ -L "$registry/current" ]; then 158 old=$(readlink -f "$registry/current") 159 case $old in "$registry"/.generations/snapshot.*) ;; *) refuse 'invalid current registry generation';; esac 160 [ "$(cat "$old/.sync-owned" 2>/dev/null)" = 1 ] || refuse 'unknown registry generation' 161 fi 162 # Snapshots that the current pointer does not name were never committed (or were superseded by a 163 # commit before their removal finished). Nothing reads them: publish.sh pins current under .sync.lock. 164 for leftover in "$registry"/.generations/snapshot.* "$registry"/.current-*; do 165 [ -e "$leftover" ] || [ -L "$leftover" ] || continue 166 [ "$leftover" != "$old" ] || continue 167 rm -rf "$leftover" 168 done 169 base=$W/base 170 mkdir -p "$base/a" "$base/show" "$base/text" 171 if [ -n "$old" ]; then 172 cp -a --reflink=auto "$old/." "$base/" 173 else 174 for dir in a text; do 175 if [ -d "$registry/$dir" ]; then cp -a --reflink=auto "$registry/$dir/." "$base/$dir/"; fi 176 done 177 fi 178 if [ -f "$registry/catalog.tsv" ]; then cp "$registry/catalog.tsv" "$base/catalog.tsv"; else cp "$HERE/research/registry/catalog.tsv" "$base/catalog.tsv"; fi 179 # A fresh registry starts with the legacy text, not an empty/unbuildable legacy corpus. 180 if [ ! -s "$base/text/spark-secrets.txt" ]; then cp "$HERE"/research/registry/text/*.txt "$base/text/"; fi 181 # Import serials: one machine counter per active generation, R0 (missing) for older generations. 182 serial=0 183 if [ -e "$base/.serial" ]; then 184 serial=$(head -c 64 "$base/.serial") 185 case $serial in ''|*[!0-9]*|0?*) refuse 'invalid generation import serial';; esac 186 [ "${#serial}" -le 19 ] || refuse 'invalid generation import serial' 187 fi 188 serial_next() { 189 # The shell counts in signed 64 bits: refuse cleanly instead of wrapping. 190 if [ "${#serial}" = 19 ] && ! printf '%s\n%s\n' "$serial" `9223372036854775806` | sort -C; then 191 echo 'research sync: import serial exhausted; request refused' >&2 192 return 1 193 fi 194 serial=$((serial + 1)) 195 } 196 date=$(date -u +%Y-%m-%d) 197 gather_one() { 198 "$SYNC" gather "$remove" "$shelf" < "$W/author" > "$W/show" || return 1 199 mv "$W/show" "$1/show/$2.tsv" 200 } 201 # layout_author FOLDER OUT: each raw file is converted alone, without policy or selection. 202 # Never use gather as a converter: a rejected copy can still carry a work-wide takedown DOI. 203 layout_author() { 204 folder=$1; out=$2 205 : > "$out" 206 for rawfile in "$folder"/*.tsv; do 207 [ -f "$rawfile" ] || continue 208 [ "${rawfile##*/}" != owner.tsv ] || continue 209 "$SYNC" layout raw < "$rawfile" > "$W/import-layout" || return 1 210 cat "$W/import-layout" >> "$out" || return 1 211 done 212 } 213 # gather TARGET [PREV_SHOW FRESH]: full selection sees all raw copies in one gather. 214 # With PREV_SHOW, only the previous selection and fresh import can win. All retained raw 215 # copies remain auxiliary evidence after X: they can deny/close work, never reselect it. 216 gather() { 217 target=$1; prev=${2:-}; fresh=${3:-} 218 rm -rf "$target/show" 219 mkdir -p "$target/show" 220 if [ -z "$prev" ]; then 221 for authorfolder in "$target"/a/*; do 222 [ -d "$authorfolder" ] || continue 223 layout_author "$authorfolder" "$W/author" || return 1 224 [ -s "$W/author" ] || continue 225 gather_one "$target" "${authorfolder##*/}" || return 1 226 done 227 return 0 228 fi 229 freshid= 230 if [ -n "$fresh" ]; then freshid=${fresh%/*}; freshid=${freshid##*/}; fi 231 for file in "$prev"/*.tsv; do 232 [ -f "$file" ] || continue 233 id=${file##*/}; id=${id%.tsv} 234 # Old multi-work selections without N bindings are refused, not guessed from shared O. 235 "$SYNC" layout selected < "$file" > "$W/author" || return 1 236 if [ "$id" = "$freshid" ]; then 237 "$SYNC" layout raw < "$fresh" > "$W/fresh-layout" || return 1 238 cat "$W/fresh-layout" >> "$W/author" || return 1 239 freshid= 240 fi 241 [ -s "$W/author" ] || continue 242 layout_author "$target/a/$id" "$W/raw-evidence" || return 1 243 printf 'X\n' >> "$W/author" || return 1 244 cat "$W/raw-evidence" >> "$W/author" || return 1 245 gather_one "$target" "$id" || return 1 246 done 247 if [ -n "$freshid" ]; then 248 "$SYNC" layout raw < "$fresh" > "$W/author" || return 1 249 layout_author "$target/a/$freshid" "$W/raw-evidence" || return 1 250 printf 'X\n' >> "$W/author" || return 1 251 cat "$W/raw-evidence" >> "$W/author" || return 1 252 gather_one "$target" "$freshid" || return 1 253 fi 254 } 255 validate() { 256 # This is the complete real publication path, including aggregate registry, documents and corpus. 257 sh "$HERE/research/publish.sh" "$W/check-site" "$W/check-corpus" "$1" > "$W/check.log" 2>&1 258 } 259 # admit TARGET PREV_SHOW FRESH: full selection, else the withholding selection. Either one must pass 260 # the real publisher. On refusal check.log holds both publisher diagnostics. 261 admit_candidate() { 262 if gather "$1" && validate "$1"; then return 0; fi 263 cp "$W/check.log" "$W/check-full.log" 2>/dev/null || : > "$W/check-full.log" 264 if [ -d "$2" ] && gather "$1" "$2" "$3" && validate "$1"; then 265 echo 'research sync: WITHHELD_RESELECTION: the full selection does not publish; an older eligible bundle stays withheld' >&2 266 sed -n '1,20p' "$W/check-full.log" >&2 267 return 0 268 fi 269 { cat "$W/check-full.log"; cat "$W/check.log" 2>/dev/null || :; } > "$W/check-both.log" 270 mv "$W/check-both.log" "$W/check.log" 271 return 1 272 } 273 # Hashed caches stay while a retained raw bundle or the selection names them. 274 gc_text() { 275 cat "$1"/a/*/*.tsv "$1"/show/*.tsv 2>/dev/null | grep "^T$TAB" | cut -f2,3 | sort -u > "$W/text-refs" || : 276 for file in "$1"/text/*.txt; do 277 [ -f "$file" ] || continue 278 stem=${file##*/}; stem=${stem%.txt}; sha=${stem##*.} 279 [ "$sha" != "$stem" ] || continue 280 case $sha in *[!0-9a-f]*) continue;; esac 281 [ "${#sha}" = 64 ] || continue 282 grep -Fxq "${stem%.*}$TAB$sha" "$W/text-refs" || rm -f "$file" 283 done 284 } 285 activate() { 286 snapshot=$(mktemp -d "$registry/.generations/snapshot.XXXXXXXX") || return 1 287 pending=$snapshot 288 cp -a --reflink=auto "$1/." "$snapshot/" || return 1 289 printf '1\n' > "$snapshot/.sync-owned" || return 1 290 linkpending=$registry/.current-$$ 291 ln -s ".generations/${snapshot##*/}" "$linkpending" || return 1 292 # ACTIVE_COMMIT: namespace-atomic pointer swap. No fsync durability claim is made here. 293 mv -Tf "$registry/.current-$$" "$registry/current" || return 1 294 pending= 295 linkpending= 296 # Compatibility links point to the same generation. Publisher pins current under .sync.lock. 297 for dir in a show text; do 298 if [ -L "$registry/$dir" ]; then 299 [ "$(readlink "$registry/$dir")" = "current/$dir" ] || refuse 'unexpected registry alias' 300 else 301 [ ! -e "$registry/$dir" ] || mv "$registry/$dir" "$W/retired-$dir" 302 ln -s "current/$dir" "$registry/$dir" 303 fi 304 done 305 if [ -n "$old" ] && [ "$old" != "$snapshot" ]; then 306 # Only the exact previous generation named by our pointer, under the writer/reader lock. 307 rm -rf "$old" 308 fi 309 old=$snapshot 310 } 311 # Operator policy boundary. research/policy.sh replaces REMOVE under REMOVE.lock and acknowledges 312 # while holding it. The worker holds the same lock from its policy read through activation, so an 313 # acknowledgement given before the worker takes the lock is in that activation, and one given after 314 # the worker releases it belongs to the next pass. Manual edits outside policy.sh get no claim. 315 policy_lock() { 316 exec 6>>"$remove.lock" || refuse 'cannot open the remove policy lock' 317 flock 6 318 } 319 policy_unlock() { 320 flock -u 6 321 exec 6>&- 322 } 323 commit_policy() { 324 rm -rf "$W/prev-show" 325 if [ -d "$1/show" ]; then cp -a "$1/show" "$W/prev-show"; else mkdir "$W/prev-show"; fi 326 if ! admit_candidate "$1" "$W/prev-show" ''; then 327 cat "$W/check.log" >&2 328 # Never leave explicitly removed work active: withhold every foreign work instead. 329 rm -rf "$1/show" 330 mkdir -p "$1/show" 331 validate "$1" || return 1 332 echo 'research sync: WITHHELD_ALL: no foreign selection publishes; foreign works withheld' >&2 333 fi 334 if [ -e "$remove" ] || [ -L "$remove" ]; then 335 version=$(sha256sum < "$remove" | cut -d ' ' -f1) || return 1 336 else 337 version=absent 338 fi 339 printf '%s\n' "$version" > "$1/.remove-sha256" 340 printf '%s\n' "$serial" > "$1/.serial" 341 gc_text "$1" 342 activate "$1" || return 1 343 echo "research sync: active remove policy sha256=$version" >&2 344 } 345 # Removals are their own transaction before any addition/network effect, so a rejected addition 346 # cannot prevent a valid takedown. Policy read errors abort without replacing derived state. 347 policy_lock 348 "$SYNC" policies "$allow" "$remove" 349 commit_policy "$base" || refuse 'existing state cannot publish; no addition attempted' 350 policy_unlock 351 [ ! -f "$state" ] || cp "$state" "$W/state" 352 "$SYNC" queue "$add" "$W/state" "$allow" "$remove" "$base/" > "$W/accepted" 353 : > "$W/pending" 354 [ ! -f "$registry/pending.tsv" ] || cat "$registry/pending.tsv" >> "$W/accepted" 355 request=0 356 recheck() { 357 request=$((request + 1)) 358 mkdir -p "$W/recheck" 359 printf '%s\n' "$1" | cut -f1-4 > "$W/recheck/queue" 360 "$SYNC" queue "$W/recheck/" "$W/recheck-state-$request" "$allow" "$remove" "$base/" > "$W/rechecked" 361 } 362 receipt() { 363 # Bounded validated request identifiers only, not stranger prose. Operator-readable job outcome. 364 printf '%s\t%s\t%s\t%s\t%s\n' "$date" "$1" "$kind" "$field1" "$did" >> "$registry/requests.tsv" 365 } 366 import_request() { 367 serial_next || return 1 368 printf '%s\n' "$serial" > "$attempt/.serial" || return 1 369 stage=$(mktemp -d "$W/fresh.XXXXXXXX") || return 1 370 case $kind in 371 Z) 372 orcid=$field1; doi=$field2 373 if [ -n "$doi" ]; then 374 rec=${doi#10.5281/zenodo.} 375 fetch "https://zenodo.org/api/records/$rec" "$W/record" || return 1 376 flatten "$W/record" "$W/record.flat" || return 1 377 if [ -z "$orcid" ]; then 378 orcid=$("$SYNC" identify < "$W/record.flat") || return 1 379 author=$(printf '%s' "$orcid" | tr X x) 380 fi 381 recheck "$(printf 'Z\t%s\t%s\t%s' "$did" "$orcid" "$doi")" || return 1 382 [ -s "$W/rechecked" ] || return 1 383 sed 's|^/|/hits/hits/0/|' "$W/record.flat" > "$W/flat" || return 1 384 "$SYNC" zenodo "$orcid" "$did" "$date" "$author" < "$W/flat" > "$W/catalog" 3> "$W/texts" || return 1 385 [ -s "$W/catalog" ] || return 1 386 while IFS="$TAB" read -r slug source; do textcopy "$slug" "$source" "$stage" || return 1; done < "$W/texts" 387 bindtext "$W/catalog" "$serial" "$stage" "$attempt/text" || return 1 388 mkdir -p "$attempt/a/$author" || return 1 389 fresh=$attempt/a/$author/doi-$rec.tsv 390 mv "$W/catalog" "$fresh" || return 1 391 else 392 zstage=$W/zstage-$request 393 sh "$HERE/research/sync.sh" --zenodo "$orcid" "$did" "$date" "$author" "$zstage/a/$author/zenodo.tsv" '' '' "$serial" || return 1 394 purge_legacy "$zstage/a/$author/zenodo.tsv" "$attempt/text" || return 1 395 mkdir -p "$attempt/a/$author" || return 1 396 fresh=$attempt/a/$author/zenodo.tsv 397 mv "$zstage/a/$author/zenodo.tsv" "$fresh" || return 1 398 for text in "$zstage"/text/*.txt; do [ ! -f "$text" ] || mv -f "$text" "$attempt/text/" || return 1; done 399 fi 400 ;; 401 P) 402 rid=$field1 403 if ! "$SYNC" onshelf "$shelf" "$rid" "$did"; then 404 echo 'research sync: repository pending or refused by shelf; retained for retry' >&2 405 printf '%s\n' "$line" >> "$W/pending" 406 receipt PENDING_SHELF 407 return 2 408 fi 409 repo=$storage/$rid 410 commit=$(env -i PATH=/usr/bin:/bin /usr/bin/git --git-dir="$repo" rev-parse --verify 'HEAD^{commit}') || return 1 411 blobread .zenodo.json "$W/meta" || return 1 412 flatten "$W/meta" "$W/flat" || return 1 413 printf '@tree\n' >> "$W/flat" 414 env -i PATH=/usr/bin:/bin /usr/bin/git --git-dir="$repo" ls-tree -l "$commit" >> "$W/flat" || return 1 415 "$SYNC" repo "$rid" "$did" "$date" "$author" plan < "$W/flat" > "$W/catalog" 3> "$W/plan" || return 1 416 while IFS="$TAB" read -r slug file open; do 417 digest=$(blobhash "$file") || return 1 418 "$SYNC" repo "$rid" "$did" "$date" "$author" "$commit" "$digest" < "$W/flat" > "$W/catalog-bound" 3> "$W/texts" || return 1 419 if [ "$open" = 1 ]; then 420 blobread "$file" "$W/paper" || return 1 421 mkdir -p "$W/fixture"; cp "$W/paper" "$W/fixture/$slug" || return 1 422 textcopy "$slug" "$file" "$stage" "$W/fixture" || return 1 423 fi 424 mv "$W/catalog-bound" "$W/catalog" || return 1 425 done < "$W/plan" 426 bindtext "$W/catalog" "$serial" "$stage" "$attempt/text" || return 1 427 mkdir -p "$attempt/a/$author" || return 1 428 fresh=$attempt/a/$author/repo-$rid.tsv 429 mv "$W/catalog" "$fresh" || return 1 430 ;; 431 *) return 1 ;; 432 esac 433 } 434 # No single request can end the pass: every outcome is recorded and the final commit below runs. 435 while IFS= read -r line; do 436 if ! recheck "$line"; then 437 echo 'research sync: request recheck failed; previous state retained' >&2 438 continue 439 fi 440 [ -s "$W/rechecked" ] || continue 441 line=$(cat "$W/rechecked") 442 kind=$(printf '%s\n' "$line" | cut -f1); did=$(printf '%s\n' "$line" | cut -f2) 443 field1=$(printf '%s\n' "$line" | cut -f3); field2=$(printf '%s\n' "$line" | cut -f4) 444 author=$(printf '%s\n' "$line" | cut -f5) 445 attempt=$W/attempt-$request 446 fresh= 447 result=0 448 if mkdir "$attempt" && cp -a --reflink=auto "$base/." "$attempt/"; then 449 import_request || result=$? 450 else 451 result=1 452 fi 453 if [ "$result" = 0 ]; then 454 if admit_candidate "$attempt" "$base/show" "$fresh"; then 455 [ "$base" = "$W/base" ] || rm -rf "$base" 456 base=$attempt 457 echo "research sync: admitted $kind $field1 requester=$did" >&2 458 receipt VALIDATED 459 else 460 echo "research sync: refused PUBLICATION_CAPACITY_OR_VALIDATION $kind $field1 requester=$did" >&2 461 receipt REFUSED_PUBLICATION 462 cat "$W/check.log" >&2 463 fi 464 elif [ "$result" != 2 ]; then 465 echo "research sync: refused METADATA_OR_TRANSPORT $kind $field1 requester=$did; previous state retained" >&2 466 receipt REFUSED_METADATA 467 fi 468 [ "$base" = "$attempt" ] || rm -rf "$attempt" 469 done < "$W/accepted" 470 # A6: reread policy and shelf, gather and validate the selected candidate again and activate it, 471 # under the operator policy lock, even when every import in this pass was refused. 472 policy_lock 473 "$SYNC" policies "$allow" "$remove" 474 commit_policy "$base" || refuse 'final selection cannot publish; previous generation retained' 475 policy_unlock 476 sort -u "$W/pending" > "$W/pending.sorted" 477 mv "$W/pending.sorted" "$registry/pending.tsv" 478 if [ -f "$W/state" ]; then 479 mv "$W/state" "$state" 480 exec 9>"$add/queue.lock" 481 flock 9 482 "$SYNC" compact "$add" "$state" 483 flock -u 9 484 exec 9>&- 485 fi