Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

128 řádky
9.8KB

  1. #!/usr/bin/env bash
  2. # Phase 3 checks for semantic retrieval (spec FR-008, FR-009, section 10): embeddings via Ollama, vectors in Qdrant,
  3. # hybrid ranking in /recall, /search and /context, isolation (AC-06), re-indexing on correction, removal on forget,
  4. # purge and new document revisions (AC-13), degradation to lexical when the provider is down (AC-16), recovery with
  5. # scripts/embed.asp, and a cold start.
  6. #
  7. # Needs the containers from docker-compose.yml running (axonasp, ollama with nomic-embed-text pulled, qdrant) and
  8. # EmbeddingProvider=ollama in public/web.config. Provisions throw-away tenants and purges them at the end. Stops and
  9. # restarts the ollama container once.
  10. # bash tests/phase3-semantic.sh
  11. set -u
  12. BASE=${BASE:-http://localhost:8801/api/v1}
  13. COLL=${COLL:-amk_nomic_embed_text_768}
  14. PASS=0; FAIL=0; FAILED=(); TENANTS=()
  15. prov() {
  16. MSYS_NO_PATHCONV=1 docker exec -u axonasp -e SERVER_WEB_ROOT=/opt/axonasp/site \
  17. -e "PROV_ACTION=$1" -e "PROV_ARG1=${2:-}" -e "PROV_ARG2=${3:-}" -e "PROV_ARG3=${4:-}" -e "PROV_ARG4=${5:-}" \
  18. axonasp ./axonasp-cli -r /opt/axonasp/site/scripts/provision.asp 2>&1 | tr -d '\r'
  19. }
  20. 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'; }
  21. field() { sed -n "s/^$1: *\([^ ]*\).*/\1/p" | head -1; }
  22. jget() { sed -n "s/.*\"$1\":\"\([^\"]*\)\".*/\1/p" | head -1; }
  23. cleanup() { docker start ollama >/dev/null 2>&1; for t in "${TENANTS[@]}"; do prov purge "$t" >/dev/null; done; }
  24. trap cleanup EXIT
  25. check() { if [ "$2" = "$3" ]; then PASS=$((PASS+1)); else FAIL=$((FAIL+1)); FAILED+=("$1 (expected $2, got $3)"); fi; }
  26. 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"; }
  27. body() { local key=$1 m=$2 p=$3; shift 3; curl -s -X "$m" -H "Authorization: Bearer $key" "$@" "$BASE$p"; }
  28. jq_() { python -I -c "import sys,json;d=json.load(sys.stdin);print($1)"; }
  29. js() { python -I -c "import json,sys;print(json.dumps(sys.argv[1]))" "$1"; }
  30. remember() { body "$1" POST /remember -d "{\"namespace_id\":\"$2\",\"type\":\"$3\",\"title\":$(js "$4"),\"content\":$(js "$5")}" | jget id; }
  31. verify() { code "$1" POST /memories/$2/verifications -d "{\"result\":\"verified\",\"method\":\"manual\",\"revision\":${3:-1}}" >/dev/null; }
  32. recall() { body "$1" POST /recall -d "{\"query\":$(js "$2")}"; }
  33. ids() { jq_ "' '.join(r.get('id', r.get('chunk_id','')) for r in d['results'])"; }
  34. # number of vector points of a tenant, optionally of one kind (asked from inside the axonasp container, which can
  35. # reach qdrant): points <tenant_id> [memory|chunk]
  36. points() {
  37. local q
  38. q=$(python -I -c "
  39. import json,sys
  40. must=[{'key':'tenant_id','match':{'value':sys.argv[1]}}]
  41. if len(sys.argv)>2 and sys.argv[2]: must.append({'key':'kind','match':{'value':sys.argv[2]}})
  42. print(json.dumps({'exact':True,'filter':{'must':must}}))" "$1" "${2:-}")
  43. docker exec axonasp wget -qO- --header='Content-Type: application/json' --post-data="$q" \
  44. "http://qdrant:6333/collections/$COLL/points/count" | python -I -c "import sys,json;print(json.load(sys.stdin)['result']['count'])"
  45. }
  46. # ---- setup ------------------------------------------------------------------------------------------------
  47. TA=$(prov tenant "p3s-A" | field tenant_id); TB=$(prov tenant "p3s-B" | field tenant_id); TENANTS=("$TA" "$TB")
  48. mk() { local out; out=$(prov principal "$1" "$2" "$3"); P_ID=$(echo "$out" | field principal_id); P_KEY=$(echo "$out" | field api_key); }
  49. mk "$TA" writer agent; WID=$P_ID; WK=$P_KEY
  50. mk "$TA" blind agent; BLID=$P_ID; BLK=$P_KEY
  51. mk "$TB" writerB agent; BID=$P_ID; BK=$P_KEY
  52. prov grant "$WID" /kb "read,write,delete,verify" 1 >/dev/null
  53. prov grant "$BID" /kb "read,write,delete,verify" 1 >/dev/null
  54. prov grant "$BLID" /elsewhere "read" 1 >/dev/null
  55. NS=$(prov namespace "$TA" /kb | field namespace_id); NSB=$(prov namespace "$TB" /kb | field namespace_id)
  56. [ -n "$NS" ] && [ -n "$NSB" ] && [ -n "$WK" ] || { echo "setup failed"; exit 2; }
  57. # ---- cold start: first request after a restart is a write that must also be embedded --------------------------
  58. docker restart axonasp >/dev/null; for i in $(seq 1 20); do curl -s -o /dev/null "$BASE/health/live" && break; sleep 1; done
  59. M1=$(remember "$WK" "$NS" semantic "Release rule" "Never deploy to production on Fridays."); verify "$WK" "$M1"
  60. check "cold: write embedded immediately" "1" "$(points "$TA" memory)"
  61. M2=$(remember "$WK" "$NS" semantic "Fruit" "Bananas are a good source of potassium."); verify "$WK" "$M2"
  62. M3=$(remember "$WK" "$NS" semantic "Staging" "The staging database is reset every night at 2am."); verify "$WK" "$M3"
  63. MB=$(remember "$BK" "$NSB" semantic "B release rule" "Tenant B never deploys to production on Fridays either."); verify "$BK" "$MB"
  64. check "all memories embedded" "3" "$(points "$TA" memory)"
  65. # ---- semantic recall without word overlap ---------------------------------------------------------------------
  66. R=$(recall "$WK" "fruit nutrition")
  67. check "paraphrase finds memory" "$M2" "$(echo "$R" | ids)"
  68. check "strategy hybrid" "hybrid" "$(echo "$R" | jq_ "d['strategy']")"
  69. check "no degradation warning" "0" "$(echo "$R" | jq_ "len(d['warnings'])")"
  70. check "second paraphrase" "$M3" "$(recall "$WK" "test environment data wipe schedule" | ids)"
  71. check "release question ranks the rule first" "$M1" "$(recall "$WK" "When are we allowed to ship a release?" | jq_ "d['results'][0]['id']")"
  72. check "unrelated query below threshold" "" "$(recall "$WK" "quantum chromodynamics lecture" | ids)"
  73. check "other tenant never sees A by meaning" "$MB" "$(recall "$BK" "When are we allowed to ship a release?" | ids)"
  74. check "principal without namespace sees nothing" "" "$(recall "$BLK" "fruit nutrition" | ids)"
  75. # ---- correction re-embeds, forget removes the point -----------------------------------------------------------
  76. 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."}')"
  77. verify "$WK" "$M2" 2
  78. check "old meaning no longer matches" "" "$(recall "$WK" "fruit nutrition" | ids)"
  79. check "new meaning matches" "$M2" "$(recall "$WK" "when is the espresso maker cleaned" | ids)"
  80. check "forget -> 200" "200" "$(code "$WK" DELETE /memories/$M3)"
  81. check "forgotten memory point removed" "2" "$(points "$TA" memory)"
  82. # ---- documents ---------------------------------------------------------------------------------------------------
  83. DOC1=$(printf '# Runbook\n\nIf the payment gateway times out, restart the checkout worker and page the on-call engineer.\n')
  84. D=$(body "$WK" POST /ingestion/jobs -d "{\"namespace_id\":\"$NS\",\"title\":\"Runbook\",\"source_uri\":\"kb://runbook\",\"format\":\"markdown\",\"content\":$(js "$DOC1")}" | jget document_id)
  85. S=$(body "$WK" POST /search -d '{"query":"what to do when card processing hangs"}')
  86. check "semantic chunk search" "yes" "$(echo "$S" | jq_ "'yes' if d['results'] and 'payment gateway' in d['results'][0]['content'] else 'no'")"
  87. check "search strategy hybrid" "hybrid" "$(echo "$S" | jq_ "d['strategy']")"
  88. P_BEFORE=$(points "$TA" chunk)
  89. DOC2=$(printf '# Runbook\n\nFor holiday rotas ask the team lead.\n')
  90. body "$WK" POST /ingestion/jobs -d "{\"namespace_id\":\"$NS\",\"title\":\"Runbook\",\"source_uri\":\"kb://runbook\",\"format\":\"markdown\",\"content\":$(js "$DOC2")}" >/dev/null
  91. check "new revision replaces old chunk points" "$P_BEFORE" "$(points "$TA" chunk)"
  92. 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'])")"
  93. # ---- context uses the same ranking -------------------------------------------------------------------------------
  94. C=$(body "$WK" POST /context -d "{\"task\":\"Plan the timing of the next production release\",\"namespace_ids\":[\"$NS\"]}")
  95. check "context finds rule by meaning" "yes" "$(echo "$C" | jq_ "'yes' if any(x['id']=='$M1' for x in d['knowledge']) else 'no'")"
  96. check "context strategy hybrid" "hybrid" "$(echo "$C" | jq_ "d['strategy']")"
  97. # ---- degradation: provider down ----------------------------------------------------------------------------------
  98. docker stop ollama >/dev/null
  99. R=$(recall "$WK" "Fridays production")
  100. check "provider down: recall still 200" "$M1" "$(echo "$R" | ids)"
  101. check "provider down: lexical strategy" "lexical" "$(echo "$R" | jq_ "d['strategy']")"
  102. check "provider down: warning" "yes" "$(echo "$R" | jq_ "'yes' if any('unavailable' in w for w in d['warnings']) else 'no'")"
  103. 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.\"}")"
  104. 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])")
  105. verify "$WK" "$M4"
  106. check "provider down: new memory not yet embedded" "2" "$(points "$TA" memory)"
  107. # a web request leaves the row pending (no attempt used); the embed-worker may already have tried it and marked it failed
  108. check "provider down: status shows pending or failed" "yes" "$(embed status | grep -qE '(pending|failed): ' && echo yes || echo no)"
  109. 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
  110. embed run >/dev/null
  111. check "embed run catches up" "3" "$(points "$TA" memory)"
  112. check "caught-up memory found by meaning" "$M4" "$(recall "$WK" "can I bring my puppy to work" | ids)"
  113. # ---- purge removes every point of the tenant ---------------------------------------------------------------------
  114. prov purge "$TB" >/dev/null; TENANTS=("$TA")
  115. check "purge removes tenant points" "0" "$(points "$TB")"
  116. echo "passed: $PASS failed: $FAIL"
  117. for f in "${FAILED[@]:-}"; do [ -n "$f" ] && echo " FAIL: $f"; done
  118. [ "$FAIL" -eq 0 ]

Powered by TurnKey Linux.