|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -euo pipefail |
| 3 | + |
| 4 | +export CI=1 |
| 5 | +export WRANGLER_SEND_METRICS=false |
| 6 | + |
| 7 | +output_dir="${DIRECT_PUBLICATION_REJECT_DETAIL_PROOF_OUTPUT:-docs/proof/direct-publication-reject-detail/artifacts}" |
| 8 | +worker_port="${DIRECT_PUBLICATION_REJECT_DETAIL_PROOF_PORT:-8797}" |
| 9 | +proof_secret="direct-publication-reject-detail-disposable-local-secret" |
| 10 | +sqlite_helper="docs/proof/direct-publication-reject-detail/assert-durable-object.mjs" |
| 11 | +state_dir="$(mktemp -d /tmp/direct-publication-reject-detail-state.XXXXXX)" |
| 12 | +wrangler_raw_log="$(mktemp /tmp/direct-publication-reject-detail-wrangler.XXXXXX)" |
| 13 | +wrangler_pid="" |
| 14 | +queue_db="" |
| 15 | + |
| 16 | +mkdir -p "$output_dir" |
| 17 | + |
| 18 | +stop_worker() { |
| 19 | + if test -n "$wrangler_pid"; then |
| 20 | + local -a worker_pids=("$wrangler_pid") |
| 21 | + local child_pid |
| 22 | + while IFS= read -r child_pid; do |
| 23 | + worker_pids+=("$child_pid") |
| 24 | + done < <( |
| 25 | + ps -eo pid=,ppid= | awk -v root="$wrangler_pid" ' |
| 26 | + { parent[$1] = $2 } |
| 27 | + END { |
| 28 | + for (pid in parent) { |
| 29 | + current = pid |
| 30 | + for (depth = 0; depth < 100 && current in parent; depth++) { |
| 31 | + current = parent[current] |
| 32 | + if (current == root) { |
| 33 | + print pid |
| 34 | + break |
| 35 | + } |
| 36 | + } |
| 37 | + } |
| 38 | + } |
| 39 | + ' |
| 40 | + ) |
| 41 | + kill "${worker_pids[@]}" >/dev/null 2>&1 || true |
| 42 | + wait "$wrangler_pid" >/dev/null 2>&1 || true |
| 43 | + wrangler_pid="" |
| 44 | + |
| 45 | + local stopped=false |
| 46 | + for _ in $(seq 1 50); do |
| 47 | + if ! curl --silent --max-time 1 "http://127.0.0.1:${worker_port}/api/health" \ |
| 48 | + >/dev/null 2>&1; then |
| 49 | + stopped=true |
| 50 | + break |
| 51 | + fi |
| 52 | + sleep 0.1 |
| 53 | + done |
| 54 | + if test "$stopped" != true; then |
| 55 | + echo "failed to stop the Wrangler process tree before restart" >&2 |
| 56 | + exit 1 |
| 57 | + fi |
| 58 | + fi |
| 59 | +} |
| 60 | + |
| 61 | +cleanup() { |
| 62 | + stop_worker |
| 63 | + sed "s/${proof_secret}/[redacted-local-proof-secret]/g" "$wrangler_raw_log" \ |
| 64 | + >"${output_dir}/wrangler.log" |
| 65 | + rm -f -- "$wrangler_raw_log" |
| 66 | + rm -rf -- "$state_dir" |
| 67 | +} |
| 68 | +trap cleanup EXIT |
| 69 | + |
| 70 | +start_worker() { |
| 71 | + npm_config_ignore_scripts=true npx --yes [email protected] dev \ |
| 72 | + --config dashboard/wrangler.toml \ |
| 73 | + --local \ |
| 74 | + --persist-to "$state_dir" \ |
| 75 | + --ip 127.0.0.1 \ |
| 76 | + --port "$worker_port" \ |
| 77 | + --var "CLAWSWEEPER_WEBHOOK_SECRET:${proof_secret}" \ |
| 78 | + >>"$wrangler_raw_log" 2>&1 & |
| 79 | + wrangler_pid=$! |
| 80 | + |
| 81 | + local ready=false |
| 82 | + for _ in $(seq 1 90); do |
| 83 | + if curl --fail --silent "http://127.0.0.1:${worker_port}/api/health" >/dev/null 2>&1; then |
| 84 | + ready=true |
| 85 | + break |
| 86 | + fi |
| 87 | + if ! kill -0 "$wrangler_pid" >/dev/null 2>&1; then |
| 88 | + sed -n '1,240p' "$wrangler_raw_log" >&2 |
| 89 | + exit 1 |
| 90 | + fi |
| 91 | + sleep 1 |
| 92 | + done |
| 93 | + test "$ready" = true |
| 94 | +} |
| 95 | + |
| 96 | +http_status() { |
| 97 | + local output_file="$1" |
| 98 | + shift |
| 99 | + curl --silent --show-error --max-time 90 --output "$output_file" --write-out '%{http_code}' "$@" |
| 100 | +} |
| 101 | + |
| 102 | +signed_post() { |
| 103 | + local body_file="$1" |
| 104 | + local output_file="$2" |
| 105 | + local signature |
| 106 | + signature="$(openssl dgst -sha256 -hmac "$proof_secret" -hex <"$body_file" | awk '{print $NF}')" |
| 107 | + http_status "$output_file" \ |
| 108 | + -X POST \ |
| 109 | + -H "content-type: application/json" \ |
| 110 | + -H "x-clawsweeper-exact-review-signature: sha256=${signature}" \ |
| 111 | + --data-binary "@${body_file}" \ |
| 112 | + "http://127.0.0.1:${worker_port}/internal/exact-review/publication-results" |
| 113 | +} |
| 114 | + |
| 115 | +pnpm install --frozen-lockfile >"${output_dir}/dependencies-install.log" 2>&1 |
| 116 | +pnpm run build:dashboard >"${output_dir}/build-dashboard.log" 2>&1 |
| 117 | + |
| 118 | +# Force the real Durable Object to initialize, then stop the entire Wrangler |
| 119 | +# process tree before inspecting its persisted schema. |
| 120 | +start_worker |
| 121 | +initial_queue_status="$(http_status "${output_dir}/initial-queue.json" \ |
| 122 | + "http://127.0.0.1:${worker_port}/api/exact-review-queue")" |
| 123 | +test "$initial_queue_status" = "200" |
| 124 | +stop_worker |
| 125 | + |
| 126 | +while IFS= read -r candidate; do |
| 127 | + if test "$(node "$sqlite_helper" "$candidate")" = "1"; then |
| 128 | + queue_db="$candidate" |
| 129 | + break |
| 130 | + fi |
| 131 | +done < <(find "$state_dir" -type f -name '*.sqlite' -print) |
| 132 | +if test -z "$queue_db"; then |
| 133 | + echo "Durable Object did not initialize: direct publication table is absent" >&2 |
| 134 | + exit 1 |
| 135 | +fi |
| 136 | + |
| 137 | +node -e ' |
| 138 | + const fs = require("node:fs"); |
| 139 | + const base = { |
| 140 | + canonicalTargetKey: "steipete/CodexBar#2797", |
| 141 | + fenceKey: "steipete/CodexBar#2797", |
| 142 | + sourceSha: "a".repeat(40), |
| 143 | + operations: [{ |
| 144 | + path: "records/steipete-CodexBar/items/2797.md", |
| 145 | + deleted: false, |
| 146 | + mode: "100644", |
| 147 | + bytes: 1, |
| 148 | + contentBase64: "eA==", |
| 149 | + }], |
| 150 | + totalBytes: 1, |
| 151 | + lifecycle: { kind: "policy_noop" }, |
| 152 | + }; |
| 153 | + const invalidRevision = { |
| 154 | + ...base, |
| 155 | + revision: 0, |
| 156 | + identity: { |
| 157 | + canonicalTargetKey: base.canonicalTargetKey, |
| 158 | + fenceKey: base.fenceKey, |
| 159 | + revision: 0, |
| 160 | + claimGeneration: 1, |
| 161 | + }, |
| 162 | + }; |
| 163 | + const outsidePath = { |
| 164 | + ...base, |
| 165 | + revision: 1, |
| 166 | + identity: { |
| 167 | + canonicalTargetKey: base.canonicalTargetKey, |
| 168 | + fenceKey: base.fenceKey, |
| 169 | + revision: 1, |
| 170 | + claimGeneration: 1, |
| 171 | + }, |
| 172 | + operations: [{ ...base.operations[0], path: "records/steipete-CodexBar/items/2798.md" }], |
| 173 | + }; |
| 174 | + fs.writeFileSync(process.argv[1], JSON.stringify(invalidRevision)); |
| 175 | + fs.writeFileSync(process.argv[2], JSON.stringify(outsidePath)); |
| 176 | +' "${output_dir}/invalid-revision-request.json" "${output_dir}/outside-path-request.json" |
| 177 | + |
| 178 | +start_worker |
| 179 | +revision_status="$(signed_post \ |
| 180 | + "${output_dir}/invalid-revision-request.json" \ |
| 181 | + "${output_dir}/invalid-revision-response.json")" |
| 182 | +path_status="$(signed_post \ |
| 183 | + "${output_dir}/outside-path-request.json" \ |
| 184 | + "${output_dir}/outside-path-response.json")" |
| 185 | +test "$revision_status" = "400" |
| 186 | +test "$path_status" = "400" |
| 187 | + |
| 188 | +REVISION_STATUS="$revision_status" PATH_STATUS="$path_status" node -e ' |
| 189 | + const assert = require("node:assert/strict"); |
| 190 | + const fs = require("node:fs"); |
| 191 | + const revision = JSON.parse(fs.readFileSync(process.argv[1], "utf8")); |
| 192 | + const outsidePath = JSON.parse(fs.readFileSync(process.argv[2], "utf8")); |
| 193 | + for (const body of [revision, outsidePath]) { |
| 194 | + assert.equal(body.error, "invalid_direct_publication_plan"); |
| 195 | + assert.equal(body.fallback_required, true); |
| 196 | + assert.equal(typeof body.detail, "string"); |
| 197 | + assert.ok(body.detail.length > 0); |
| 198 | + } |
| 199 | + assert.match(revision.detail, /invalid direct publication revision/); |
| 200 | + assert.match(outsidePath.detail, /direct publication path is outside steipete-CodexBar#2797/); |
| 201 | + assert.notEqual(revision.detail, outsidePath.detail); |
| 202 | + const lines = [ |
| 203 | + "Durable Object initialized: exact_review_direct_publication_plans table present", |
| 204 | + "invalid revision: HTTP " + process.env.REVISION_STATUS + "; error=" + revision.error + "; fallback_required=" + revision.fallback_required + "; detail=" + revision.detail, |
| 205 | + "outside path: HTTP " + process.env.PATH_STATUS + "; error=" + outsidePath.error + "; fallback_required=" + outsidePath.fallback_required + "; detail=" + outsidePath.detail, |
| 206 | + "distinct validation details: true", |
| 207 | + ]; |
| 208 | + fs.writeFileSync(process.argv[3], lines.join("\n") + "\n"); |
| 209 | +' \ |
| 210 | + "${output_dir}/invalid-revision-response.json" \ |
| 211 | + "${output_dir}/outside-path-response.json" \ |
| 212 | + "${output_dir}/runtime-transcript.txt" |
| 213 | + |
| 214 | +test -s "${output_dir}/runtime-transcript.txt" |
| 215 | +cat "${output_dir}/runtime-transcript.txt" |
0 commit comments