interplane-forge
INTERPLANE Forge
Brought by drakestapleton
interplane-forge / research / publish.sh
| 1 | #!/bin/sh |
| 2 | # research/publish.sh [OUT [CORPUS [REGISTRY [TEXT]]]] |
| 3 | # Foreign inputs are ONLY REGISTRY/show/<author-id>.tsv, after sync gather's removals. |
| 4 | # Raw a/ catalogs are not publication inputs. Omega owns merge, IDs and corpus admission. |
| 5 | # |
| 6 | # Publication is one immutable generation and one pointer switch: |
| 7 | # OUT/gen/public.<unique>/ index.html index.json works.bib agents.txt research.css add.html |
| 8 | # doi.html a/<id>.html w/<slug>.{html,json,bib} corpus.txt |
| 9 | # OUT/current -> gen/public.<unique> replaced by rename(2) only after the generation is complete and synced |
| 10 | # OUT/<leaf> -> current/<leaf> fixed compatibility links for every top-level generation entry |
| 11 | # CORPUS -> <OUT absolute>/current/corpus.txt the agent's corpus path follows the same pointer |
| 12 | # Mutable state stays outside generations: OUT/ask/ and OUT/add/ are never written or pruned here. |
| 13 | # forged (research/route.om) reads OUT/current once per request and opens a path inside that |
| 14 | # generation. All old generations are retained, including paths not yet opened by a reader. |
| 15 | # A killed publisher leaves the previous current generation complete; its staging leftovers are removed by the next run under OUT/.publish.lock. |
| 16 | set -eu |
| 17 | export LC_ALL=C |
| 18 | cd "$(dirname "$0")/.." |
| 19 | OMC=$HOME/work/flux/omega-mv/seed/out/omc |
| 20 | PIN=3ad9f10f64309e7acf8a725082b28e23a2a4aeb07e98322fb4dc0fcd8fc93194 |
| 21 | [ "$(sha256sum "$OMC" | cut -d' ' -f1)" = "$PIN" ] || { echo 'publish.sh: omc is not the pin' >&2; exit 2; } |
| 22 | OUT=${1:-$HOME/forge-data/research} |
| 23 | CORPUS=${2:-$HOME/forge-research/corpus.txt} |
| 24 | OUT=$(realpath -ms "$OUT") |
| 25 | CORPUS=$(realpath -ms "$CORPUS") |
| 26 | # Only the fixed OUT/corpus.txt alias or a separate external path is a corpus API. |
| 27 | case $CORPUS in "$OUT/corpus.txt") ;; "$OUT"/*) echo 'publish.sh: corpus path overlaps public tree' >&2; exit 2 ;; esac |
| 28 | REGISTRY=${3:-${RESEARCH_REGISTRY:-research/registry}} |
| 29 | # Pin the complete admitted generation while holding the reader/writer registry lock. |
| 30 | LOCKROOT=$REGISTRY |
| 31 | mkdir -p "$LOCKROOT" |
| 32 | exec 7>"$LOCKROOT/.sync.lock" |
| 33 | flock 7 |
| 34 | if [ -L "$REGISTRY/current" ]; then |
| 35 | REGISTRY=$(readlink -f "$REGISTRY/current") |
| 36 | fi |
| 37 | TEXT=${4:-$REGISTRY/text} |
| 38 | B=$(mktemp -d "${TMPDIR:-/tmp}/research-publish.XXXXXX") |
| 39 | S= |
| 40 | trap 'rm -rf "$B"; [ -z "$S" ] || rm -rf "$S"' EXIT |
| 41 | trap 'exit 1' HUP INT TERM |
| 42 | mkdir -p "$OUT/gen" "$OUT/ask" "$(dirname "$CORPUS")" "$B/site/w" "$B/site/a" |
| 43 | OUTABS=$(cd "$OUT" && pwd -P) |
| 44 | G=$OUT/gen |
| 45 | exec 9>"$OUT/.publish.lock" |
| 46 | flock 9 |
| 47 | # Recovery removes only incomplete staging names, never a named public generation. |
| 48 | cur= |
| 49 | if [ -L "$OUT/current" ]; then |
| 50 | cur=$(readlink "$OUT/current") |
| 51 | case $cur in gen/public.*) ;; *) echo 'publish.sh: invalid current generation link' >&2; exit 1 ;; esac |
| 52 | case ${cur#gen/} in *[!a-zA-Z0-9.-]*) echo 'publish.sh: invalid generation leaf' >&2; exit 1 ;; esac |
| 53 | cur=${cur#gen/} |
| 54 | [ -d "$G/$cur" ] || { echo 'publish.sh: current generation is missing' >&2; exit 1; } |
| 55 | elif [ -e "$OUT/current" ]; then |
| 56 | echo 'publish.sh: OUT/current is not a link' >&2; exit 1 |
| 57 | fi |
| 58 | for d in "$G"/.build.* "$OUT"/.current.*; do |
| 59 | [ -e "$d" ] || [ -L "$d" ] || continue |
| 60 | rm -rf "$d" |
| 61 | done |
| 62 | |
| 63 | "$OMC" < research/build.om > "$B/build" |
| 64 | chmod 700 "$B/build" |
| 65 | C=$B/catalog.tsv |
| 66 | cp "$REGISTRY/catalog.tsv" "$C" |
| 67 | for f in "$REGISTRY"/show/*.tsv; do |
| 68 | [ -f "$f" ] || continue |
| 69 | [ -s "$f" ] || continue |
| 70 | bytes=$(( $(wc -c < "$f") + 8 )) |
| 71 | [ "$bytes" -le 262143 ] || { echo 'publish.sh: capacity: author catalog exceeds builder input 262143 bytes' >&2; exit 4; } |
| 72 | { printf 'M\tmerge\n'; cat "$f"; } | "$B/build" > "$B/merge" |
| 73 | printf '\n' >> "$C" |
| 74 | cat "$B/merge" >> "$C" |
| 75 | done |
| 76 | gen() { |
| 77 | prefix=$(printf 'M\t%s\t%s\n' "$1" "$2" | wc -c) |
| 78 | bytes=$(( $(wc -c < "$C") + prefix )) |
| 79 | [ "$bytes" -le 262143 ] || { echo "publish.sh: capacity: aggregate catalog exceeds builder input 262143 bytes mode=$1" >&2; exit 4; } |
| 80 | { printf 'M\t%s\t%s\n' "$1" "$2"; cat "$C"; } | "$B/build" > "$3" |
| 81 | } |
| 82 | gen slugs '' "$B/slugs" |
| 83 | gen authors '' "$B/authors" |
| 84 | gen foreign '' "$B/foreign" |
| 85 | gen corpus '' "$B/texts" |
| 86 | gen index '' "$B/site/index.html" |
| 87 | gen json '' "$B/site/index.json" |
| 88 | gen bib '' "$B/site/works.bib" |
| 89 | gen txt '' "$B/site/agents.txt" |
| 90 | gen css '' "$B/site/research.css" |
| 91 | gen add '' "$B/site/add.html" |
| 92 | gen doi '' "$B/site/doi.html" |
| 93 | while IFS= read -r id; do |
| 94 | gen author "$id" "$B/site/a/$id.html" |
| 95 | done < "$B/authors" |
| 96 | n=0 |
| 97 | while IFS= read -r s; do |
| 98 | gen work "$s" "$B/site/w/$s.html" |
| 99 | gen wjson "$s" "$B/site/w/$s.json" |
| 100 | gen wbib "$s" "$B/site/w/$s.bib" |
| 101 | n=$((n + 1)) |
| 102 | done < "$B/slugs" |
| 103 | : > "$B/corpus" |
| 104 | tab=$(printf '\t') |
| 105 | while IFS="$tab" read -r s title identity; do |
| 106 | if grep -Fxq "$s" "$B/foreign"; then |
| 107 | case $identity in ''|-) continue ;; esac |
| 108 | t=$TEXT/$s.$identity.txt |
| 109 | [ -s "$t" ] || continue |
| 110 | [ "$(sha256sum "$t" | cut -d' ' -f1)" = "$identity" ] || { echo 'publish.sh: text cache digest mismatch' >&2; exit 1; } |
| 111 | printf '@@\t%s\t%s\n' "$s" "$title" >> "$B/corpus" |
| 112 | "$B/build" quote < "$t" >> "$B/corpus" |
| 113 | else |
| 114 | t=$REGISTRY/text/$s.txt |
| 115 | [ -s "$t" ] || { echo "publish.sh: no full text for legacy work $s" >&2; exit 1; } |
| 116 | printf '@@\t%s\t%s\n' "$s" "$title" >> "$B/corpus" |
| 117 | cat "$t" >> "$B/corpus" |
| 118 | fi |
| 119 | printf '\n' >> "$B/corpus" |
| 120 | done < "$B/texts" |
| 121 | [ "$(wc -c < "$B/corpus")" -le 4194303 ] || { echo "publish.sh: corpus exceeds 4194303 bytes" >&2; exit 1; } |
| 122 | headers=$(grep -c '^@@[[:space:]]' "$B/corpus" || :) |
| 123 | [ "$headers" -le 256 ] || { echo 'publish.sh: corpus exceeds 256 works' >&2; exit 1; } |
| 124 | # Stage the complete generation, then make its bytes durable before anything can point at it. |
| 125 | S=$(mktemp -d "$G/.build.XXXXXX") |
| 126 | chmod 755 "$S" |
| 127 | cp -R "$B/site/." "$S/" |
| 128 | cp "$B/corpus" "$S/corpus.txt" |
| 129 | find "$S" -type f -exec chmod 444 {} + |
| 130 | find "$S" -exec sync {} + |
| 131 | id=public.${S##*.} |
| 132 | # Commit preparation: name the immutable tree. No old generation is ever removed. |
| 133 | mv -T "$S" "$G/$id" |
| 134 | S= |
| 135 | sync "$G" |
| 136 | # Compatibility links never name a generation, so they never change after their first install. |
| 137 | alias_to() { |
| 138 | if [ -L "$1" ] && [ "$(readlink "$1")" = "$2" ]; then return 0; fi |
| 139 | ln -s "$2" "$1.lnk.$$" |
| 140 | if [ -d "$1" ] && [ ! -L "$1" ]; then |
| 141 | # One-time migration of the old in-place directory; the router never reads it. |
| 142 | mv -T "$1" "$1.legacy.$$" |
| 143 | mv -T "$1.lnk.$$" "$1" |
| 144 | rm -rf "$1.legacy.$$" |
| 145 | else |
| 146 | mv -Tf "$1.lnk.$$" "$1" |
| 147 | fi |
| 148 | } |
| 149 | for p in "$G/$id"/*; do |
| 150 | leaf=${p##*/} |
| 151 | alias_to "$OUT/$leaf" "current/$leaf" |
| 152 | done |
| 153 | if [ "$CORPUS" != "$OUT/corpus.txt" ]; then alias_to "$CORPUS" "$OUTABS/current/corpus.txt"; fi |
| 154 | rm -f "$OUT/generated.manifest" |
| 155 | # ATOMIC_PUBLIC_COMMIT: the sole visibility switch, after aliases and all bytes exist. |
| 156 | ln -s "gen/$id" "$OUT/.current.$$" |
| 157 | mv -Tf "$OUT/.current.$$" "$OUT/current" |
| 158 | sync "$OUT" |
| 159 | echo "published $n works to $OUT/gen/$id; corpus $(wc -c < "$CORPUS") bytes" |