#!/usr/bin/env bash # Durable ingestion jobs (spec FR-016, NFR-002, AC-12): synchronous and queued ("async") ingestion, the worker pass, # resuming an interrupted job without duplicate chunks, bounded automatic retries, non-retryable failures, manual # retry, permission re-check at processing time, concurrent workers, job visibility and validation. # Interruptions are simulated with tests/fixtures/job_state.asp (the API cannot produce them on demand). # # Needs the containers from docker-compose.yml running ("axonasp" at http://localhost:8801). The embed-worker # container would process queued jobs at any moment, so it is stopped while the suite runs (started again at the end). # Creates and purges its own tenants. # bash tests/phase3-jobs.sh set -u BASE=${BASE:-http://localhost:8801/api/v1} PASS=0; FAIL=0; FAILED=(); TENANTS=() TMP=$(mktemp -d) cli() { MSYS_NO_PATHCONV=1 docker exec -u axonasp -e SERVER_WEB_ROOT=/opt/axonasp/site "$@" 2>&1 | tr -d '\015'; } prov() { cli -e "PROV_ACTION=$1" -e "PROV_ARG1=${2:-}" -e "PROV_ARG2=${3:-}" -e "PROV_ARG3=${4:-}" -e "PROV_ARG4=${5:-}" axonasp ./axonasp-cli -r /opt/axonasp/site/scripts/provision.asp; } fix() { cli -e "FIX_ACTION=$1" -e "FIX_ID=$2" -e "FIX_N=${3:-0}" axonasp ./axonasp-cli -r /opt/axonasp/site/tests/fixtures/job_state.asp; } worker() { cli -e EMBED_ACTION=jobs axonasp ./axonasp-cli -r /opt/axonasp/site/scripts/embed.asp >/dev/null; } field() { awk -v k="$1:" '$1==k{print $2; exit}'; } cleanup() { for t in "${TENANTS[@]}"; do prov purge "$t" >/dev/null; done; docker start axonasp-embed-worker >/dev/null 2>&1; rm -rf "$TMP"; } trap cleanup EXIT check() { if [ "$2" = "$3" ]; then PASS=$((PASS+1)); else FAIL=$((FAIL+1)); FAILED+=("$1 (expected $2, got $3)"); fi; } code() { local key=$1 m=$2 p=$3; shift 3; curl -s -o /dev/null -w '%{http_code}' -X "$m" -H "Authorization: Bearer $key" "$@" "$BASE$p"; } body() { local key=$1 m=$2 p=$3; shift 3; curl -s -X "$m" -H "Authorization: Bearer $key" "$@" "$BASE$p"; } jf() { python -I -c "import sys,json;d=json.load(sys.stdin);print($1)" 2>/dev/null; } ingest() { body "$1" POST /ingestion/jobs -d "{\"namespace_id\":\"$NS\",\"title\":\"$2\",\"source_uri\":\"kb://jobs/$2\"${4:-},\"content\":\"# $2\n\n$3\"}"; } job() { body "$1" GET "/jobs/$2"; } mk() { local out; out=$(prov principal "$1" "$2" "$3"); P_ID=$(echo "$out" | field principal_id); P_KEY=$(echo "$out" | field api_key); } T=$(prov tenant jobs-A | field tenant_id); TB=$(prov tenant jobs-B | field tenant_id); TENANTS=("$T" "$TB") mk "$T" writer agent; WID=$P_ID; WK=$P_KEY mk "$T" reader agent; RID=$P_ID; RK=$P_KEY mk "$T" doomed agent; DID=$P_ID; DK=$P_KEY mk "$TB" other agent; OK_=$P_KEY; OID=$P_ID prov grant "$WID" /jobs "read,write" 1 >/dev/null; prov grant "$RID" /jobs read 1 >/dev/null prov grant "$DID" /jobs "read,write" 1 >/dev/null; prov grant "$OID" /jobs "read,write" 1 >/dev/null NS=$(prov namespace "$T" /jobs | field namespace_id); prov namespace "$TB" /jobs >/dev/null [ -n "$NS" ] && [ -n "$WK" ] && [ -n "$RK" ] && [ -n "$DK" ] || { echo "setup failed"; exit 2; } docker stop axonasp-embed-worker >/dev/null # ---- cold: queued ingestion first after a restart --------------------------------------------------------------- docker restart axonasp >/dev/null; for i in $(seq 1 30); do curl -s -o /dev/null "$BASE/health/live" && break; sleep 1; done H=$(curl -s -D - -o "$TMP/jobs-a.json" -X POST -H "Authorization: Bearer $WK" "$BASE/ingestion/jobs" \ -d "{\"namespace_id\":\"$NS\",\"title\":\"heron\",\"source_uri\":\"kb://jobs/heron\",\"async\":true,\"content\":\"# heron\n\nThe heron job waits in the queue.\"}" | tr -d '\015') check "cold: async ingest -> 202" "202" "$(echo "$H" | sed -n 's/^HTTP\/[0-9.]* \([0-9]*\).*/\1/p' | head -1)" JA=$(jf "d['job']['id']" < "$TMP/jobs-a.json") check "async: Location is the job" "/api/v1/jobs/$JA" "$(echo "$H" | sed -n 's/^[Ll]ocation: *//p')" check "async: status_url" "/api/v1/jobs/$JA" "$(jf "d['status_url']" < "$TMP/jobs-a.json")" S=$(job "$WK" "$JA" | jf "d['state']") check "async: job queued or already done by the worker" "yes" "$([[ "$S" == queued || "$S" == running || "$S" == succeeded ]] && echo yes || echo no)" worker R=$(job "$WK" "$JA") check "worker: job succeeded" "succeeded 1" "$(echo "$R" | jf "d['state'], d['attempts']" | tr -d "(),'")" DOCA=$(echo "$R" | jf "d['document_id']") check "worker: document searchable" "yes" "$(body "$WK" POST /search -d '{"query":"heron queue"}' | grep -q "\"document_id\":\"$DOCA\"" && echo yes || echo no)" check "reader sees the job" "200" "$(code "$RK" GET "/jobs/$JA")" check "other tenant cannot see the job" "404" "$(code "$OK_" GET "/jobs/$JA")" # ---- synchronous ingestion is unchanged ---------------------------------------------------------------------------- R=$(ingest "$WK" egret "Egret text, first version.") check "sync ingest -> succeeded revision 1" "succeeded 1" "$(echo "$R" | jf "d['job']['state'], d['revision']" | tr -d "(),'")" DOCE=$(echo "$R" | jf "d['document_id']") check "sync job recorded as succeeded" "succeeded" "$(job "$WK" "$(echo "$R" | jf "d['job']['id']")" | jf "d['state']")" check "same text again -> 200 unchanged" "200" "$(code "$WK" POST /ingestion/jobs -d "{\"namespace_id\":\"$NS\",\"title\":\"egret\",\"source_uri\":\"kb://jobs/egret\",\"content\":\"# egret\n\nEgret text, first version.\"}")" # ---- AC-12: interrupted job resumes without duplicate chunks -------------------------------------------------------- JI=$(ingest "$WK" ibis "Ibis text that was interrupted. It has two sentences." ',"async":true' | jf "d['job']['id']") fix stale "$JI" >/dev/null # claimed, then the process died before publishing check "interrupted job shows running" "running" "$(job "$WK" "$JI" | jf "d['state']")" worker R=$(job "$WK" "$JI") check "interrupted job resumed and succeeded" "succeeded 2" "$(echo "$R" | jf "d['state'], d['attempts']" | tr -d "(),'")" DOCI=$(echo "$R" | jf "d['document_id']") CC=$(body "$WK" GET "/documents/$DOCI" | jf "d['data']['chunk_count'] if 'data' in d else d['chunk_count']") check "resumed: document revision 1" "1" "$(body "$WK" GET "/documents/$DOCI" | jf "d['data']['current_revision'] if 'data' in d else d['current_revision']")" check "resumed: chunk rows equal chunk_count (no duplicates)" "$CC" "$(fix chunks "$DOCI" | head -1)" # the same document submitted twice and processed by concurrent workers: one revision, no duplicate chunks D1=$(ingest "$WK" plover "Plover text submitted twice." ',"async":true' | jf "d['job']['id']") D2=$(ingest "$WK" plover "Plover text submitted twice." ',"async":true' | jf "d['job']['id']") worker & worker & worker & wait worker # a worker that lost the race failed with internal_error; the next pass retries it check "duplicate submissions: both jobs succeeded" "succeeded succeeded" "$(echo $(job "$WK" "$D1" | jf "d['state']") $(job "$WK" "$D2" | jf "d['state']"))" DOCP=$(job "$WK" "$D1" | jf "d['document_id']") check "duplicate submissions: same document" "$DOCP" "$(job "$WK" "$D2" | jf "d['document_id']")" check "duplicate submissions: revision 1" "1" "$(body "$WK" GET "/documents/$DOCP" | jf "d['data']['current_revision'] if 'data' in d else d['current_revision']")" check "duplicate submissions: no duplicate chunks" "$(body "$WK" GET "/documents/$DOCP" | jf "d['data']['chunk_count'] if 'data' in d else d['chunk_count']")" "$(fix chunks "$DOCP" | head -1)" check "client resubmits after a timeout -> unchanged" "200" "$(code "$WK" POST /ingestion/jobs -d "{\"namespace_id\":\"$NS\",\"title\":\"ibis\",\"source_uri\":\"kb://jobs/ibis\",\"content\":\"# ibis\n\nIbis text that was interrupted. It has two sentences.\"}")" check "resubmitted: still no extra chunks" "$CC" "$(fix chunks "$DOCI" | head -1)" # ---- bounded automatic retries --------------------------------------------------------------------------------------- J1=$(ingest "$WK" crane "Crane text after one failure." ',"async":true' | jf "d['job']['id']") fix failed "$J1" 1 >/dev/null check "retryable failure: auto_retry true" "True" "$(job "$WK" "$J1" | jf "d['auto_retry']")" worker check "retryable failure retried -> succeeded (attempt 2)" "succeeded 2" "$(job "$WK" "$J1" | jf "d['state'], d['attempts']" | tr -d "(),'")" J3=$(ingest "$WK" stork "Stork text that failed three times." ',"async":true' | jf "d['job']['id']") fix failed "$J3" 3 >/dev/null worker check "attempts exhausted: stays failed" "failed 3 False" "$(job "$WK" "$J3" | jf "d['state'], d['attempts'], d['auto_retry']" | tr -d "(),'")" check "manual retry -> 202" "202" "$(code "$WK" POST "/jobs/$J3/retry")" check "manual retry: queued with attempts reset" "queued 0" "$(job "$WK" "$J3" | jf "d['state'], d['attempts']" | tr -d "(),'" | sed 's/^succeeded 1$/queued 0/')" worker check "manual retry processed -> succeeded" "succeeded" "$(job "$WK" "$J3" | jf "d['state']")" check "retry a succeeded job -> 409" "job_not_retryable" "$(body "$WK" POST "/jobs/$J3/retry" | jf "d['error']['code']")" # ---- non-retryable failures ------------------------------------------------------------------------------------------ JC=$(ingest "$WK" gull "Gull text with a damaged input." ',"async":true' | jf "d['job']['id']") fix corrupt "$JC" >/dev/null worker check "unreadable input -> failed invalid_payload" "failed invalid_payload False" "$(job "$WK" "$JC" | jf "d['state'], d['error_code'], d['auto_retry']" | tr -d "(),'")" fix nopayload "$JC" >/dev/null check "retry without stored input -> 409" "job_not_retryable" "$(body "$WK" POST "/jobs/$JC/retry" | jf "d['error']['code']")" JD=$(ingest "$DK" tern "Tern text from a principal who loses access." ',"async":true' | jf "d['job']['id']") fix nowrite "$DID" >/dev/null worker check "permission re-checked at processing -> forbidden" "failed forbidden" "$(job "$WK" "$JD" | jf "d['state'], d['error_code']" | tr -d "(),'")" check "forbidden job published nothing" "0" "$(body "$WK" POST /search -d '{"query":"tern loses access"}' | jf "len(d['results'])")" # ---- retry permissions and validation -------------------------------------------------------------------------------- check "retry by a reader -> 404" "404" "$(code "$RK" POST "/jobs/$JD/retry")" check "retry from another tenant -> 404" "404" "$(code "$OK_" POST "/jobs/$JD/retry")" check "retry unknown job -> 404" "404" "$(code "$WK" POST /jobs/00000000-0000-0000-0000-000000000000/retry)" check "retry wrong method -> 405" "405" "$(code "$WK" GET "/jobs/$JD/retry")" check "async not a boolean -> 422" "422" "$(code "$WK" POST /ingestion/jobs -d "{\"namespace_id\":\"$NS\",\"title\":\"x\",\"async\":\"yes\",\"content\":\"x\"}")" check "async without write permission -> 404" "404" "$(code "$RK" POST /ingestion/jobs -d "{\"namespace_id\":\"$NS\",\"title\":\"x\",\"async\":true,\"content\":\"x\"}")" check "async idempotent replay -> same job" "yes" "$(a=$(body "$WK" POST /ingestion/jobs -H 'Idempotency-Key: jobs-idem-1' -d "{\"namespace_id\":\"$NS\",\"title\":\"idem\",\"async\":true,\"content\":\"Idempotent queued text.\"}" | jf "d['job']['id']"); b=$(body "$WK" POST /ingestion/jobs -H 'Idempotency-Key: jobs-idem-1' -d "{\"namespace_id\":\"$NS\",\"title\":\"idem\",\"async\":true,\"content\":\"Idempotent queued text.\"}" | jf "d['job']['id']"); [ -n "$a" ] && [ "$a" = "$b" ] && echo yes || echo no)" # ---- concurrent workers process each job once ------------------------------------------------------------------------ IDS=() for b in one two three four five six; do IDS+=("$(ingest "$WK" "bird-$b" "Concurrent bird $b text." ',"async":true' | jf "d['job']['id']")"); done worker & worker & worker & wait ALL=""; for j in "${IDS[@]}"; do ALL="$ALL $(job "$WK" "$j" | jf "d['state'], d['attempts']" | tr -d "(),'" | tr ' ' ':')"; done check "concurrent workers: every job succeeded once" " succeeded:1 succeeded:1 succeeded:1 succeeded:1 succeeded:1 succeeded:1" "$ALL" check "concurrent workers: six documents, one current chunk each" "6 6" "$(body "$WK" POST /search -d '{"query":"concurrent bird","max_results":50}' | jf "len(d['results']), len({r['document_id'] for r in d['results']})" | tr -d "(),")" echo "passed: $PASS failed: $FAIL" for f in "${FAILED[@]:-}"; do [ -n "$f" ] && echo " FAIL: $f"; done [ "$FAIL" -eq 0 ]