interplane.Get Omega
Read in your language

interplane-forge

INTERPLANE Forge

Brought by drakestapleton

branch main Branches Tags History

interplane-forge / research / publish.sh

159 lines · 6764 bytes Raw Blame History
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.
16set -eu
17export LC_ALL=C
18cd "$(dirname "$0")/.."
19OMC=$HOME/work/flux/omega-mv/seed/out/omc
20PIN=3ad9f10f64309e7acf8a725082b28e23a2a4aeb07e98322fb4dc0fcd8fc93194
21[ "$(sha256sum "$OMC" | cut -d' ' -f1)" = "$PIN" ] || { echo 'publish.sh: omc is not the pin' >&2; exit 2; }
22OUT=${1:-$HOME/forge-data/research}
23CORPUS=${2:-$HOME/forge-research/corpus.txt}
24OUT=$(realpath -ms "$OUT")
25CORPUS=$(realpath -ms "$CORPUS")
26# Only the fixed OUT/corpus.txt alias or a separate external path is a corpus API.
27case $CORPUS in "$OUT/corpus.txt") ;; "$OUT"/*) echo 'publish.sh: corpus path overlaps public tree' >&2; exit 2 ;; esac
28REGISTRY=${3:-${RESEARCH_REGISTRY:-research/registry}}
29# Pin the complete admitted generation while holding the reader/writer registry lock.
30LOCKROOT=$REGISTRY
31mkdir -p "$LOCKROOT"
32exec 7>"$LOCKROOT/.sync.lock"
33flock 7
34if [ -L "$REGISTRY/current" ]; then
35 REGISTRY=$(readlink -f "$REGISTRY/current")
36fi
37TEXT=${4:-$REGISTRY/text}
38B=$(mktemp -d "${TMPDIR:-/tmp}/research-publish.XXXXXX")
39S=
40trap 'rm -rf "$B"; [ -z "$S" ] || rm -rf "$S"' EXIT
41trap 'exit 1' HUP INT TERM
42mkdir -p "$OUT/gen" "$OUT/ask" "$(dirname "$CORPUS")" "$B/site/w" "$B/site/a"
43OUTABS=$(cd "$OUT" && pwd -P)
44G=$OUT/gen
45exec 9>"$OUT/.publish.lock"
46flock 9
47# Recovery removes only incomplete staging names, never a named public generation.
48cur=
49if [ -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; }
55elif [ -e "$OUT/current" ]; then
56 echo 'publish.sh: OUT/current is not a link' >&2; exit 1
57fi
58for d in "$G"/.build.* "$OUT"/.current.*; do
59 [ -e "$d" ] || [ -L "$d" ] || continue
60 rm -rf "$d"
61done
62
63"$OMC" < research/build.om > "$B/build"
64chmod 700 "$B/build"
65C=$B/catalog.tsv
66cp "$REGISTRY/catalog.tsv" "$C"
67for 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"
75done
76gen() {
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}
82gen slugs '' "$B/slugs"
83gen authors '' "$B/authors"
84gen foreign '' "$B/foreign"
85gen corpus '' "$B/texts"
86gen index '' "$B/site/index.html"
87gen json '' "$B/site/index.json"
88gen bib '' "$B/site/works.bib"
89gen txt '' "$B/site/agents.txt"
90gen css '' "$B/site/research.css"
91gen add '' "$B/site/add.html"
92gen doi '' "$B/site/doi.html"
93while IFS= read -r id; do
94 gen author "$id" "$B/site/a/$id.html"
95done < "$B/authors"
96n=0
97while 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))
102done < "$B/slugs"
103: > "$B/corpus"
104tab=$(printf '\t')
105while 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"
120done < "$B/texts"
121[ "$(wc -c < "$B/corpus")" -le 4194303 ] || { echo "publish.sh: corpus exceeds 4194303 bytes" >&2; exit 1; }
122headers=$(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.
125S=$(mktemp -d "$G/.build.XXXXXX")
126chmod 755 "$S"
127cp -R "$B/site/." "$S/"
128cp "$B/corpus" "$S/corpus.txt"
129find "$S" -type f -exec chmod 444 {} +
130find "$S" -exec sync {} +
131id=public.${S##*.}
132# Commit preparation: name the immutable tree. No old generation is ever removed.
133mv -T "$S" "$G/$id"
134S=
135sync "$G"
136# Compatibility links never name a generation, so they never change after their first install.
137alias_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}
149for p in "$G/$id"/*; do
150 leaf=${p##*/}
151 alias_to "$OUT/$leaf" "current/$leaf"
152done
153if [ "$CORPUS" != "$OUT/corpus.txt" ]; then alias_to "$CORPUS" "$OUTABS/current/corpus.txt"; fi
154rm -f "$OUT/generated.manifest"
155# ATOMIC_PUBLIC_COMMIT: the sole visibility switch, after aliases and all bytes exist.
156ln -s "gen/$id" "$OUT/.current.$$"
157mv -Tf "$OUT/.current.$$" "$OUT/current"
158sync "$OUT"
159echo "published $n works to $OUT/gen/$id; corpus $(wc -c < "$CORPUS") bytes"