No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

145 líneas
14KB

  1. #!/usr/bin/env bash
  2. # Consolidation checks (spec section 12, FR-013, AC-08, AC-16): POST /consolidations groups similar experiences of a
  3. # namespace without any generative model, returns a draft per group, and with "propose": true files proposed
  4. # candidates citing the experiences as evidence. Covers grouping and drafts, thresholds, no duplicate proposals,
  5. # review by another principal and recall of the accepted lesson, permissions (404), tenant isolation (AC-06),
  6. # validation (422/400), wrong method (405), UTF-8 and a cold first request.
  7. #
  8. # Needs the container from docker-compose.yml running. Provisions two throw-away tenants and purges them at the end.
  9. # bash tests/phase3-consolidation.sh
  10. set -u
  11. BASE=${BASE:-http://localhost:8801/api/v1}
  12. PASS=0; FAIL=0; FAILED=(); TENANTS=()
  13. TMP=$(mktemp -d)
  14. prov() {
  15. MSYS_NO_PATHCONV=1 docker exec -u axonasp -e SERVER_WEB_ROOT=/opt/axonasp/site \
  16. -e "PROV_ACTION=$1" -e "PROV_ARG1=${2:-}" -e "PROV_ARG2=${3:-}" -e "PROV_ARG3=${4:-}" -e "PROV_ARG4=${5:-}" \
  17. axonasp ./axonasp-cli -r /opt/axonasp/site/scripts/provision.asp 2>&1 | tr -d '\015'
  18. }
  19. field() { sed -n "s/^$1: *\([^ ]*\).*/\1/p" | head -1; }
  20. cleanup() { for t in "${TENANTS[@]}"; do prov purge "$t" >/dev/null; done; rm -rf "$TMP"; }
  21. trap cleanup EXIT
  22. check() { if [ "$2" = "$3" ]; then PASS=$((PASS+1)); else FAIL=$((FAIL+1)); FAILED+=("$1 (expected $2, got $3)"); fi; }
  23. 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"; }
  24. body() { local key=$1 m=$2 p=$3; shift 3; curl -s -X "$m" -H "Authorization: Bearer $key" "$@" "$BASE$p"; }
  25. jq_() { python -I -c "import sys,json;sys.stdin.reconfigure(encoding='utf-8');d=json.load(sys.stdin);print($1)"; }
  26. mk() { local out; out=$(prov principal "$1" "$2" "$3"); P_ID=$(echo "$out" | field principal_id); P_KEY=$(echo "$out" | field api_key); }
  27. session() { body "$1" POST /sessions -d "{\"namespace_id\":\"$2\",\"objective\":\"$3\"}" | jq_ "d['id']"; }
  28. # event <key> <session> <outcome> <task> <problem> <action> <result> (sent as UTF-8 from a file)
  29. event() {
  30. python -I -c "import json,sys;a=sys.argv;open(a[1],'w',encoding='utf-8').write(json.dumps({'task':a[3],'outcome':a[2],'problem':a[4],'action':a[5],'result':a[6]},ensure_ascii=False))" \
  31. "$TMP/ev.json" "$3" "$4" "$5" "$6" "$7"
  32. body "$1" POST /sessions/$2/events --data-binary @"$TMP/ev.json" | jq_ "d['id']"
  33. }
  34. cons() { body "$1" POST /consolidations -d "$2"; }
  35. # ---- setup ------------------------------------------------------------------------------------------------------
  36. TA=$(prov tenant "cons-A" | field tenant_id); TB=$(prov tenant "cons-B" | field tenant_id); TENANTS=("$TA" "$TB")
  37. mk "$TA" agent1 agent; W1=$P_ID; WK=$P_KEY
  38. mk "$TA" agent2 agent; W2=$P_ID; WK2=$P_KEY
  39. mk "$TA" reviewer human; RV=$P_ID; RK=$P_KEY
  40. mk "$TA" reader agent; RD=$P_ID; DK=$P_KEY
  41. mk "$TB" agentB agent; BW=$P_ID; BK=$P_KEY
  42. prov grant "$W1" /ops "read,write,verify" 1 >/dev/null
  43. prov grant "$W2" /ops "read,write" 1 >/dev/null
  44. prov grant "$RV" /ops "read,verify" 1 >/dev/null
  45. prov grant "$RD" /ops "read" 1 >/dev/null
  46. prov grant "$BW" /ops "read,write" 1 >/dev/null
  47. NS=$(prov namespace "$TA" /ops | field namespace_id); NS2=$(prov namespace "$TA" /ops/other | field namespace_id)
  48. NSB=$(prov namespace "$TB" /ops | field namespace_id)
  49. [ -n "$NS" ] && [ -n "$NSB" ] && [ -n "$WK" ] || { echo "setup failed"; exit 2; }
  50. S1=$(session "$WK" "$NS" "Release week 1"); S2=$(session "$WK2" "$NS" "Release week 2"); S3=$(session "$WK" "$NS2" "Elsewhere")
  51. SB=$(session "$BK" "$NSB" "Tenant B")
  52. # group P: port conflict on deploy (2 failures, 2 successes, 2 sessions)
  53. P1=$(event "$WK" "$S1" failure "Deploy quillfeather service" "Port 7100 already in use by the metrics agent" "Restarted the quillfeather container" "Container failed again, port 7100 still bound")
  54. P2=$(event "$WK" "$S1" success "Deploy quillfeather service" "Port 7100 already in use by the metrics agent" "Moved quillfeather to port 7101 in compose" "Deploy succeeded on port 7101")
  55. P3=$(event "$WK2" "$S2" failure "Deploy quillfeather service after upgrade" "Port 7100 conflict with metrics agent again" "Killed the metrics agent process" "Metrics agent restarted and took port 7100 back")
  56. P4=$(event "$WK2" "$S2" success "Deploy quillfeather service after upgrade" "Port 7100 conflict with the metrics agent" "Pinned quillfeather to port 7101" "Deploy worked, port 7101")
  57. # group C: certificate renewal (3 successes, 2 sessions)
  58. C1=$(event "$WK" "$S1" success "Renew TLS certificate for gateway" "Certificate expires in 5 days" "Ran certbot renew with the dns challenge" "New certificate installed, gateway reloaded")
  59. C2=$(event "$WK2" "$S2" success "Renew TLS certificate for gateway" "Certificate close to expiry" "certbot renew dns challenge" "Certificate renewed and gateway reloaded")
  60. C3=$(event "$WK2" "$S2" success "Renew gateway TLS certificate" "Expiry warning from monitoring" "certbot renew using dns challenge" "Renewed, gateway reload done")
  61. # unrelated
  62. event "$WK" "$S1" success "Update team wiki page" "Outdated onboarding steps" "Rewrote the onboarding section" "Wiki updated" >/dev/null
  63. event "$WK" "$S1" partial "Tune database vacuum schedule" "Nightly vacuum too slow" "Moved vacuum to sundays" "Faster weekdays, weekend still slow" >/dev/null
  64. event "$WK2" "$S2" unknown "Investigate flaky login test" "Test fails one run in ten" "Added retries and logging" "Not reproduced yet" >/dev/null
  65. # same story in another namespace and another tenant: must not be grouped with /ops
  66. event "$WK" "$S3" failure "Deploy quillfeather service" "Port 7100 already in use by the metrics agent" "Restarted the container" "Failed again" >/dev/null
  67. event "$BK" "$SB" failure "Deploy quillfeather service" "Port 7100 already in use by the metrics agent" "Restarted the container" "Failed again" >/dev/null
  68. event "$BK" "$SB" success "Deploy quillfeather service" "Port 7100 already in use by the metrics agent" "Moved to port 7101" "Worked" >/dev/null
  69. [ -n "$P4" ] && [ -n "$C3" ] || { echo "event setup failed"; exit 2; }
  70. # ---- cold start: first request after a restart ----------------------------------------------------------------------
  71. docker restart axonasp >/dev/null; for i in $(seq 1 20); do curl -s -o /dev/null "$BASE/health/live" && break; sleep 1; done
  72. cons "$DK" "{\"namespace_id\":\"$NS\"}" > "$TMP/r.json"
  73. check "cold: dry run by a reader -> 2 groups from 10 experiences" "10 2 0" "$(jq_ "str(d['experiences_considered'])+' '+str(len(d['groups']))+' '+str(d['candidates_created'])" < "$TMP/r.json")"
  74. check "largest group is the port conflict" "yes" "$(jq_ "'yes' if sorted(d['groups'][0]['experience_ids'])==sorted(['$P1','$P2','$P3','$P4']) else 'no'" < "$TMP/r.json")"
  75. check "second group is the certificate renewal" "yes" "$(jq_ "'yes' if sorted(d['groups'][1]['experience_ids'])==sorted(['$C1','$C2','$C3']) else 'no'" < "$TMP/r.json")"
  76. check "group sessions and outcomes" "2 2 2 0 0" "$(jq_ "' '.join(str(x) for x in [d['groups'][0]['sessions']]+[d['groups'][0]['outcomes'][k] for k in ['success','failure','partial','unknown']])" < "$TMP/r.json")"
  77. check "draft types: lesson for mixed outcomes, solution for successes" "lesson solution" "$(jq_ "d['groups'][0]['draft']['type']+' '+d['groups'][1]['draft']['type']" < "$TMP/r.json")"
  78. check "draft title from the newest task" "Recurring: Deploy quillfeather service after upgrade" "$(jq_ "d['groups'][0]['draft']['title']" < "$TMP/r.json")"
  79. check "draft lists what failed and what worked" "yes" "$(jq_ "(lambda c: 'yes' if 'What did not work:' in c and 'Killed the metrics agent process' in c and 'What worked:' in c and 'Pinned quillfeather to port 7101' in c and 'Seen 4 times in 2 session(s)' in c else 'no')(d['groups'][0]['draft']['content'])" < "$TMP/r.json")"
  80. check "solution draft has no failure section" "no" "$(jq_ "'yes' if 'What did not work' in d['groups'][1]['draft']['content'] else 'no'" < "$TMP/r.json")"
  81. check "terms of the port group" "yes" "$(jq_ "'yes' if {'port','7100'} <= set(d['groups'][0]['terms']) else 'no'" < "$TMP/r.json")"
  82. check "first_seen <= last_seen" "True" "$(jq_ "d['groups'][0]['first_seen']<=d['groups'][0]['last_seen']" < "$TMP/r.json")"
  83. check "dry run creates no candidates" "0" "$(body "$WK" GET "/candidates?namespace_id=$NS" | jq_ "len(d['data'])")"
  84. check "other namespace and tenant not mixed in" "yes" "$(jq_ "'yes' if all(len(g['experience_ids'])<=4 for g in d['groups']) else 'no'" < "$TMP/r.json")"
  85. # ---- thresholds and filters -------------------------------------------------------------------------------------------
  86. check "min_size 4 -> only the port group" "1 4" "$(cons "$DK" "{\"namespace_id\":\"$NS\",\"min_size\":4}" | jq_ "str(len(d['groups']))+' '+str(d['groups'][0]['size'])")"
  87. check "similarity 0.95 -> no groups" "0" "$(cons "$DK" "{\"namespace_id\":\"$NS\",\"similarity\":0.95}" | jq_ "len(d['groups'])")"
  88. check "since in the future -> nothing considered" "0 0" "$(cons "$DK" "{\"namespace_id\":\"$NS\",\"since\":\"2099-01-01\"}" | jq_ "str(d['experiences_considered'])+' '+str(len(d['groups']))")"
  89. check "max_experiences limits the input" "10" "$(cons "$DK" "{\"namespace_id\":\"$NS\",\"max_experiences\":10}" | jq_ "d['experiences_considered']")"
  90. check "tenant B sees only its own story" "1 2" "$(cons "$BK" "{\"namespace_id\":\"$NSB\"}" | jq_ "str(len(d['groups']))+' '+str(d['groups'][0]['size'])")"
  91. # ---- permissions and validation ---------------------------------------------------------------------------------------
  92. check "reader cannot propose -> 404" "404" "$(code "$DK" POST /consolidations -d "{\"namespace_id\":\"$NS\",\"propose\":true}")"
  93. check "other tenant's namespace -> 404" "404" "$(code "$BK" POST /consolidations -d "{\"namespace_id\":\"$NS\"}")"
  94. check "unknown namespace -> 404" "404" "$(code "$WK" POST /consolidations -d '{"namespace_id":"00000000-0000-4000-8000-000000000000"}')"
  95. check "missing namespace_id -> 422" "422" "$(code "$WK" POST /consolidations -d '{}')"
  96. check "similarity out of range -> 422" "422" "$(code "$WK" POST /consolidations -d "{\"namespace_id\":\"$NS\",\"similarity\":2}")"
  97. check "min_size 1 -> 422" "422" "$(code "$WK" POST /consolidations -d "{\"namespace_id\":\"$NS\",\"min_size\":1}")"
  98. check "max_experiences 5 -> 422" "422" "$(code "$WK" POST /consolidations -d "{\"namespace_id\":\"$NS\",\"max_experiences\":5}")"
  99. check "min_size not a number -> 422" "422" "$(code "$WK" POST /consolidations -d "{\"namespace_id\":\"$NS\",\"min_size\":\"2\"}")"
  100. check "propose not boolean -> 422" "422" "$(code "$WK" POST /consolidations -d "{\"namespace_id\":\"$NS\",\"propose\":\"yes\"}")"
  101. check "bad since -> 422" "422" "$(code "$WK" POST /consolidations -d "{\"namespace_id\":\"$NS\",\"since\":\"last week\"}")"
  102. check "invalid JSON -> 400" "400" "$(code "$WK" POST /consolidations -d '{"namespace_id":')"
  103. check "empty body -> 400" "400" "$(code "$WK" POST /consolidations)"
  104. check "no auth -> 401" "401" "$(curl -s -o /dev/null -w '%{http_code}' -X POST -d '{}' "$BASE/consolidations")"
  105. check "wrong method -> 405" "405" "$(code "$WK" GET /consolidations)"
  106. # ---- propose ------------------------------------------------------------------------------------------------------------
  107. cons "$WK" "{\"namespace_id\":\"$NS\",\"propose\":true}" > "$TMP/p.json"
  108. check "propose -> 2 candidates" "2" "$(jq_ "d['candidates_created']" < "$TMP/p.json")"
  109. CP=$(jq_ "d['groups'][0]['candidate_id']" < "$TMP/p.json")
  110. R=$(body "$RK" GET /candidates/$CP)
  111. check "candidate is proposed, tagged and authored by the caller" "proposed ['consolidated'] lesson" "$(echo "$R" | jq_ "(d.get('data') or d)['state']+' '+str((d.get('data') or d)['tags'])+' '+(d.get('data') or d)['type']")"
  112. check "candidate cites the 4 experiences" "4" "$(echo "$R" | jq_ "len([e for e in (d.get('data') or d)['evidence'] if e['kind']=='experience'])")"
  113. check "propose again -> nothing new" "0 4" "$(cons "$WK" "{\"namespace_id\":\"$NS\",\"propose\":true}" | jq_ "str(d['candidates_created'])+' '+str(d['groups'][0]['already_cited'])")"
  114. P5=$(event "$WK" "$S1" failure "Deploy quillfeather service" "Port 7100 already in use by the metrics agent" "Restarted the quillfeather container" "Port 7100 still bound")
  115. check "one new experience is not enough for a new proposal" "0 5" "$(cons "$WK" "{\"namespace_id\":\"$NS\",\"propose\":true}" | jq_ "str(d['candidates_created'])+' '+str(d['groups'][0]['size'])")"
  116. P6=$(event "$WK2" "$S2" success "Deploy quillfeather service" "Port 7100 conflict with the metrics agent" "Pinned quillfeather to port 7101" "Deploy worked on port 7101")
  117. cons "$WK" "{\"namespace_id\":\"$NS\",\"propose\":true}" > "$TMP/p2.json"
  118. check "two new experiences -> a new proposal" "1" "$(jq_ "d['candidates_created']" < "$TMP/p2.json")"
  119. # ---- review and reuse ------------------------------------------------------------------------------------------------
  120. check "proposer with verify cannot approve its own candidate -> 403" "403" "$(code "$WK" POST /candidates/$CP/review -d '{"decision":"accept","verify":true}')"
  121. R=$(body "$RK" POST /candidates/$CP/review -d '{"decision":"accept","verify":true}')
  122. MEM=$(echo "$R" | jq_ "(d.get('data') or d).get('memory_id') or (d.get('data') or d).get('resulting_memory_id') or ''")
  123. check "reviewer accepts -> memory created" "yes" "$([ -n "$MEM" ] && echo yes || echo no)"
  124. check "accepted lesson is recalled" "yes" "$(body "$DK" POST /recall -d '{"query":"quillfeather port 7100 metrics agent"}' | jq_ "'yes' if any(r['id']=='$MEM' for r in d['results']) else 'no'")"
  125. check "memory sources cite the experiences" "4" "$(body "$DK" GET /memories/$MEM/sources | jq_ "len([e for e in d['evidence'] if e['kind']=='experience'])")"
  126. # ---- UTF-8 ------------------------------------------------------------------------------------------------------------
  127. NSU=$(prov namespace "$TA" /ops/utf | field namespace_id); SU=$(session "$WK" "$NSU" "utf")
  128. for oc in failure success; do
  129. python -I -c "import json,sys;t='Kalibrierung der W'+chr(228)+'rmepumpe pr'+chr(252)+'fen';open(sys.argv[1],'w',encoding='utf-8').write(json.dumps({'task':t,'outcome':sys.argv[2],'problem':'Sensor meldet '+chr(0x65e5)+' Fehler','action':'Neustart '+sys.argv[2],'result':'Fehler '+sys.argv[2]},ensure_ascii=False))" "$TMP/ev.json" "$oc"
  130. body "$WK" POST /sessions/$SU/events --data-binary @"$TMP/ev.json" >/dev/null
  131. done
  132. check "UTF-8 round trip in the draft title" "yes" "$(cons "$WK" "{\"namespace_id\":\"$NSU\"}" | jq_ "'yes' if d['groups'][0]['draft']['title']=='Recurring: Kalibrierung der W'+chr(228)+'rmepumpe pr'+chr(252)+'fen' else 'no'")"
  133. echo "passed: $PASS failed: $FAIL"
  134. for f in "${FAILED[@]}"; do echo " FAIL: $f"; done
  135. [ "$FAIL" -eq 0 ]

Powered by TurnKey Linux.