|
- #!/usr/bin/env bash
- # Phase 3 checks for semantic retrieval (spec FR-008, FR-009, section 10): embeddings via Ollama, vectors in Qdrant,
- # hybrid ranking in /recall, /search and /context, isolation (AC-06), re-indexing on correction, removal on forget,
- # purge and new document revisions (AC-13), degradation to lexical when the provider is down (AC-16), recovery with
- # scripts/embed.asp, and a cold start.
- #
- # Needs the containers from docker-compose.yml running (axonasp, ollama with nomic-embed-text pulled, qdrant) and
- # EmbeddingProvider=ollama in public/web.config. Provisions throw-away tenants and purges them at the end. Stops and
- # restarts the ollama container once.
- # bash tests/phase3-semantic.sh
- set -u
- BASE=${BASE:-http://localhost:8801/api/v1}
- COLL=${COLL:-amk_nomic_embed_text_768}
- PASS=0; FAIL=0; FAILED=(); TENANTS=()
- prov() {
- MSYS_NO_PATHCONV=1 docker exec -u axonasp -e SERVER_WEB_ROOT=/opt/axonasp/site \
- -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 2>&1 | tr -d '\r'
- }
- embed() { MSYS_NO_PATHCONV=1 docker exec -u axonasp -e SERVER_WEB_ROOT=/opt/axonasp/site -e "EMBED_ACTION=$1" axonasp ./axonasp-cli -r /opt/axonasp/site/scripts/embed.asp 2>&1 | tr -d '\r'; }
- field() { sed -n "s/^$1: *\([^ ]*\).*/\1/p" | head -1; }
- jget() { sed -n "s/.*\"$1\":\"\([^\"]*\)\".*/\1/p" | head -1; }
- cleanup() { docker start ollama >/dev/null 2>&1; for t in "${TENANTS[@]}"; do prov purge "$t" >/dev/null; done; }
- 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"; }
- jq_() { python -I -c "import sys,json;d=json.load(sys.stdin);print($1)"; }
- js() { python -I -c "import json,sys;print(json.dumps(sys.argv[1]))" "$1"; }
- remember() { body "$1" POST /remember -d "{\"namespace_id\":\"$2\",\"type\":\"$3\",\"title\":$(js "$4"),\"content\":$(js "$5")}" | jget id; }
- verify() { code "$1" POST /memories/$2/verifications -d "{\"result\":\"verified\",\"method\":\"manual\",\"revision\":${3:-1}}" >/dev/null; }
- recall() { body "$1" POST /recall -d "{\"query\":$(js "$2")}"; }
- ids() { jq_ "' '.join(r.get('id', r.get('chunk_id','')) for r in d['results'])"; }
- # number of vector points of a tenant, optionally of one kind (asked from inside the axonasp container, which can
- # reach qdrant): points <tenant_id> [memory|chunk]
- points() {
- local q
- q=$(python -I -c "
- import json,sys
- must=[{'key':'tenant_id','match':{'value':sys.argv[1]}}]
- if len(sys.argv)>2 and sys.argv[2]: must.append({'key':'kind','match':{'value':sys.argv[2]}})
- print(json.dumps({'exact':True,'filter':{'must':must}}))" "$1" "${2:-}")
- docker exec axonasp wget -qO- --header='Content-Type: application/json' --post-data="$q" \
- "http://qdrant:6333/collections/$COLL/points/count" | python -I -c "import sys,json;print(json.load(sys.stdin)['result']['count'])"
- }
-
- # ---- setup ------------------------------------------------------------------------------------------------
- TA=$(prov tenant "p3s-A" | field tenant_id); TB=$(prov tenant "p3s-B" | field tenant_id); TENANTS=("$TA" "$TB")
- mk() { local out; out=$(prov principal "$1" "$2" "$3"); P_ID=$(echo "$out" | field principal_id); P_KEY=$(echo "$out" | field api_key); }
- mk "$TA" writer agent; WID=$P_ID; WK=$P_KEY
- mk "$TA" blind agent; BLID=$P_ID; BLK=$P_KEY
- mk "$TB" writerB agent; BID=$P_ID; BK=$P_KEY
- prov grant "$WID" /kb "read,write,delete,verify" 1 >/dev/null
- prov grant "$BID" /kb "read,write,delete,verify" 1 >/dev/null
- prov grant "$BLID" /elsewhere "read" 1 >/dev/null
- NS=$(prov namespace "$TA" /kb | field namespace_id); NSB=$(prov namespace "$TB" /kb | field namespace_id)
- [ -n "$NS" ] && [ -n "$NSB" ] && [ -n "$WK" ] || { echo "setup failed"; exit 2; }
-
- # ---- cold start: first request after a restart is a write that must also be embedded --------------------------
- docker restart axonasp >/dev/null; for i in $(seq 1 20); do curl -s -o /dev/null "$BASE/health/live" && break; sleep 1; done
- M1=$(remember "$WK" "$NS" semantic "Release rule" "Never deploy to production on Fridays."); verify "$WK" "$M1"
- check "cold: write embedded immediately" "1" "$(points "$TA" memory)"
- M2=$(remember "$WK" "$NS" semantic "Fruit" "Bananas are a good source of potassium."); verify "$WK" "$M2"
- M3=$(remember "$WK" "$NS" semantic "Staging" "The staging database is reset every night at 2am."); verify "$WK" "$M3"
- MB=$(remember "$BK" "$NSB" semantic "B release rule" "Tenant B never deploys to production on Fridays either."); verify "$BK" "$MB"
- check "all memories embedded" "3" "$(points "$TA" memory)"
-
- # ---- semantic recall without word overlap ---------------------------------------------------------------------
- R=$(recall "$WK" "fruit nutrition")
- check "paraphrase finds memory" "$M2" "$(echo "$R" | ids)"
- check "strategy hybrid" "hybrid" "$(echo "$R" | jq_ "d['strategy']")"
- check "no degradation warning" "0" "$(echo "$R" | jq_ "len(d['warnings'])")"
- check "second paraphrase" "$M3" "$(recall "$WK" "test environment data wipe schedule" | ids)"
- check "release question ranks the rule first" "$M1" "$(recall "$WK" "When are we allowed to ship a release?" | jq_ "d['results'][0]['id']")"
- check "unrelated query below threshold" "" "$(recall "$WK" "quantum chromodynamics lecture" | ids)"
- check "other tenant never sees A by meaning" "$MB" "$(recall "$BK" "When are we allowed to ship a release?" | ids)"
- check "principal without namespace sees nothing" "" "$(recall "$BLK" "fruit nutrition" | ids)"
-
- # ---- correction re-embeds, forget removes the point -----------------------------------------------------------
- check "correct -> 200" "200" "$(code "$WK" PATCH /memories/$M2 -H 'If-Match: 1' -d '{"title":"Coffee machine","content":"The office coffee machine is descaled every Monday."}')"
- verify "$WK" "$M2" 2
- check "old meaning no longer matches" "" "$(recall "$WK" "fruit nutrition" | ids)"
- check "new meaning matches" "$M2" "$(recall "$WK" "when is the espresso maker cleaned" | ids)"
- check "forget -> 200" "200" "$(code "$WK" DELETE /memories/$M3)"
- check "forgotten memory point removed" "2" "$(points "$TA" memory)"
-
- # ---- documents ---------------------------------------------------------------------------------------------------
- DOC1=$(printf '# Runbook\n\nIf the payment gateway times out, restart the checkout worker and page the on-call engineer.\n')
- D=$(body "$WK" POST /ingestion/jobs -d "{\"namespace_id\":\"$NS\",\"title\":\"Runbook\",\"source_uri\":\"kb://runbook\",\"format\":\"markdown\",\"content\":$(js "$DOC1")}" | jget document_id)
- S=$(body "$WK" POST /search -d '{"query":"what to do when card processing hangs"}')
- check "semantic chunk search" "yes" "$(echo "$S" | jq_ "'yes' if d['results'] and 'payment gateway' in d['results'][0]['content'] else 'no'")"
- check "search strategy hybrid" "hybrid" "$(echo "$S" | jq_ "d['strategy']")"
- P_BEFORE=$(points "$TA" chunk)
- DOC2=$(printf '# Runbook\n\nFor holiday rotas ask the team lead.\n')
- body "$WK" POST /ingestion/jobs -d "{\"namespace_id\":\"$NS\",\"title\":\"Runbook\",\"source_uri\":\"kb://runbook\",\"format\":\"markdown\",\"content\":$(js "$DOC2")}" >/dev/null
- check "new revision replaces old chunk points" "$P_BEFORE" "$(points "$TA" chunk)"
- check "old revision text not found by meaning" "0" "$(body "$WK" POST /search -d '{"query":"what to do when card processing hangs"}' | jq_ "len(d['results'])")"
-
- # ---- context uses the same ranking -------------------------------------------------------------------------------
- C=$(body "$WK" POST /context -d "{\"task\":\"Plan the timing of the next production release\",\"namespace_ids\":[\"$NS\"]}")
- check "context finds rule by meaning" "yes" "$(echo "$C" | jq_ "'yes' if any(x['id']=='$M1' for x in d['knowledge']) else 'no'")"
- check "context strategy hybrid" "hybrid" "$(echo "$C" | jq_ "d['strategy']")"
-
- # ---- degradation: provider down ----------------------------------------------------------------------------------
- docker stop ollama >/dev/null
- R=$(recall "$WK" "Fridays production")
- check "provider down: recall still 200" "$M1" "$(echo "$R" | ids)"
- check "provider down: lexical strategy" "lexical" "$(echo "$R" | jq_ "d['strategy']")"
- check "provider down: warning" "yes" "$(echo "$R" | jq_ "'yes' if any('unavailable' in w for w in d['warnings']) else 'no'")"
- check "provider down: remember still 201" "201" "$(code "$WK" POST /remember -d "{\"namespace_id\":\"$NS\",\"type\":\"semantic\",\"title\":\"Pets\",\"content\":\"Dogs are allowed in the office on Thursdays.\"}")"
- M4=$(body "$WK" GET "/memories?namespace_id=$NS&limit=50" | python -I -c "import sys,json;print([m['id'] for m in json.load(sys.stdin)['data'] if m['title']=='Pets'][0])")
- verify "$WK" "$M4"
- check "provider down: new memory not yet embedded" "2" "$(points "$TA" memory)"
- check "provider down: status shows failed" "yes" "$(embed status | grep -q 'failed: ' && echo yes || echo no)"
- docker start ollama >/dev/null; for i in $(seq 1 30); do docker exec axonasp wget -qO- http://ollama:11434/api/version >/dev/null 2>&1 && break; sleep 1; done
- embed run >/dev/null
- check "embed run catches up" "3" "$(points "$TA" memory)"
- check "caught-up memory found by meaning" "$M4" "$(recall "$WK" "can I bring my puppy to work" | ids)"
-
- # ---- purge removes every point of the tenant ---------------------------------------------------------------------
- prov purge "$TB" >/dev/null; TENANTS=("$TA")
- check "purge removes tenant points" "0" "$(points "$TB")"
-
- echo "passed: $PASS failed: $FAIL"
- for f in "${FAILED[@]:-}"; do [ -n "$f" ] && echo " FAIL: $f"; done
- [ "$FAIL" -eq 0 ]
|