|
- #!/usr/bin/env bash
- # Consolidation checks (spec section 12, FR-013, AC-08, AC-16): POST /consolidations groups similar experiences of a
- # namespace without any generative model, returns a draft per group, and with "propose": true files proposed
- # candidates citing the experiences as evidence. Covers grouping and drafts, thresholds, no duplicate proposals,
- # review by another principal and recall of the accepted lesson, permissions (404), tenant isolation (AC-06),
- # validation (422/400), wrong method (405), UTF-8 and a cold first request.
- #
- # Needs the container from docker-compose.yml running. Provisions two throw-away tenants and purges them at the end.
- # bash tests/phase3-consolidation.sh
- set -u
- BASE=${BASE:-http://localhost:8801/api/v1}
- PASS=0; FAIL=0; FAILED=(); TENANTS=()
- TMP=$(mktemp -d)
- 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 '\015'
- }
- field() { sed -n "s/^$1: *\([^ ]*\).*/\1/p" | head -1; }
- cleanup() { for t in "${TENANTS[@]}"; do prov purge "$t" >/dev/null; done; 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"; }
- jq_() { python -I -c "import sys,json;sys.stdin.reconfigure(encoding='utf-8');d=json.load(sys.stdin);print($1)"; }
- mk() { local out; out=$(prov principal "$1" "$2" "$3"); P_ID=$(echo "$out" | field principal_id); P_KEY=$(echo "$out" | field api_key); }
- session() { body "$1" POST /sessions -d "{\"namespace_id\":\"$2\",\"objective\":\"$3\"}" | jq_ "d['id']"; }
- # event <key> <session> <outcome> <task> <problem> <action> <result> (sent as UTF-8 from a file)
- event() {
- 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))" \
- "$TMP/ev.json" "$3" "$4" "$5" "$6" "$7"
- body "$1" POST /sessions/$2/events --data-binary @"$TMP/ev.json" | jq_ "d['id']"
- }
- cons() { body "$1" POST /consolidations -d "$2"; }
-
- # ---- setup ------------------------------------------------------------------------------------------------------
- TA=$(prov tenant "cons-A" | field tenant_id); TB=$(prov tenant "cons-B" | field tenant_id); TENANTS=("$TA" "$TB")
- mk "$TA" agent1 agent; W1=$P_ID; WK=$P_KEY
- mk "$TA" agent2 agent; W2=$P_ID; WK2=$P_KEY
- mk "$TA" reviewer human; RV=$P_ID; RK=$P_KEY
- mk "$TA" reader agent; RD=$P_ID; DK=$P_KEY
- mk "$TB" agentB agent; BW=$P_ID; BK=$P_KEY
- prov grant "$W1" /ops "read,write,verify" 1 >/dev/null
- prov grant "$W2" /ops "read,write" 1 >/dev/null
- prov grant "$RV" /ops "read,verify" 1 >/dev/null
- prov grant "$RD" /ops "read" 1 >/dev/null
- prov grant "$BW" /ops "read,write" 1 >/dev/null
- NS=$(prov namespace "$TA" /ops | field namespace_id); NS2=$(prov namespace "$TA" /ops/other | field namespace_id)
- NSB=$(prov namespace "$TB" /ops | field namespace_id)
- [ -n "$NS" ] && [ -n "$NSB" ] && [ -n "$WK" ] || { echo "setup failed"; exit 2; }
-
- S1=$(session "$WK" "$NS" "Release week 1"); S2=$(session "$WK2" "$NS" "Release week 2"); S3=$(session "$WK" "$NS2" "Elsewhere")
- SB=$(session "$BK" "$NSB" "Tenant B")
- # group P: port conflict on deploy (2 failures, 2 successes, 2 sessions)
- 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")
- 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")
- 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")
- 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")
- # group C: certificate renewal (3 successes, 2 sessions)
- 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")
- C2=$(event "$WK2" "$S2" success "Renew TLS certificate for gateway" "Certificate close to expiry" "certbot renew dns challenge" "Certificate renewed and gateway reloaded")
- C3=$(event "$WK2" "$S2" success "Renew gateway TLS certificate" "Expiry warning from monitoring" "certbot renew using dns challenge" "Renewed, gateway reload done")
- # unrelated
- event "$WK" "$S1" success "Update team wiki page" "Outdated onboarding steps" "Rewrote the onboarding section" "Wiki updated" >/dev/null
- event "$WK" "$S1" partial "Tune database vacuum schedule" "Nightly vacuum too slow" "Moved vacuum to sundays" "Faster weekdays, weekend still slow" >/dev/null
- event "$WK2" "$S2" unknown "Investigate flaky login test" "Test fails one run in ten" "Added retries and logging" "Not reproduced yet" >/dev/null
- # same story in another namespace and another tenant: must not be grouped with /ops
- event "$WK" "$S3" failure "Deploy quillfeather service" "Port 7100 already in use by the metrics agent" "Restarted the container" "Failed again" >/dev/null
- event "$BK" "$SB" failure "Deploy quillfeather service" "Port 7100 already in use by the metrics agent" "Restarted the container" "Failed again" >/dev/null
- event "$BK" "$SB" success "Deploy quillfeather service" "Port 7100 already in use by the metrics agent" "Moved to port 7101" "Worked" >/dev/null
- [ -n "$P4" ] && [ -n "$C3" ] || { echo "event setup failed"; exit 2; }
-
- # ---- cold start: first request after a restart ----------------------------------------------------------------------
- docker restart axonasp >/dev/null; for i in $(seq 1 20); do curl -s -o /dev/null "$BASE/health/live" && break; sleep 1; done
- cons "$DK" "{\"namespace_id\":\"$NS\"}" > "$TMP/r.json"
- 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")"
- 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")"
- 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")"
- 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")"
- 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")"
- check "draft title from the newest task" "Recurring: Deploy quillfeather service after upgrade" "$(jq_ "d['groups'][0]['draft']['title']" < "$TMP/r.json")"
- 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")"
- 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")"
- check "terms of the port group" "yes" "$(jq_ "'yes' if {'port','7100'} <= set(d['groups'][0]['terms']) else 'no'" < "$TMP/r.json")"
- check "first_seen <= last_seen" "True" "$(jq_ "d['groups'][0]['first_seen']<=d['groups'][0]['last_seen']" < "$TMP/r.json")"
- check "dry run creates no candidates" "0" "$(body "$WK" GET "/candidates?namespace_id=$NS" | jq_ "len(d['data'])")"
- 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")"
-
- # ---- thresholds and filters -------------------------------------------------------------------------------------------
- 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'])")"
- check "similarity 0.95 -> no groups" "0" "$(cons "$DK" "{\"namespace_id\":\"$NS\",\"similarity\":0.95}" | jq_ "len(d['groups'])")"
- 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']))")"
- check "max_experiences limits the input" "10" "$(cons "$DK" "{\"namespace_id\":\"$NS\",\"max_experiences\":10}" | jq_ "d['experiences_considered']")"
- 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'])")"
-
- # ---- permissions and validation ---------------------------------------------------------------------------------------
- check "reader cannot propose -> 404" "404" "$(code "$DK" POST /consolidations -d "{\"namespace_id\":\"$NS\",\"propose\":true}")"
- check "other tenant's namespace -> 404" "404" "$(code "$BK" POST /consolidations -d "{\"namespace_id\":\"$NS\"}")"
- check "unknown namespace -> 404" "404" "$(code "$WK" POST /consolidations -d '{"namespace_id":"00000000-0000-4000-8000-000000000000"}')"
- check "missing namespace_id -> 422" "422" "$(code "$WK" POST /consolidations -d '{}')"
- check "similarity out of range -> 422" "422" "$(code "$WK" POST /consolidations -d "{\"namespace_id\":\"$NS\",\"similarity\":2}")"
- check "min_size 1 -> 422" "422" "$(code "$WK" POST /consolidations -d "{\"namespace_id\":\"$NS\",\"min_size\":1}")"
- check "max_experiences 5 -> 422" "422" "$(code "$WK" POST /consolidations -d "{\"namespace_id\":\"$NS\",\"max_experiences\":5}")"
- check "min_size not a number -> 422" "422" "$(code "$WK" POST /consolidations -d "{\"namespace_id\":\"$NS\",\"min_size\":\"2\"}")"
- check "propose not boolean -> 422" "422" "$(code "$WK" POST /consolidations -d "{\"namespace_id\":\"$NS\",\"propose\":\"yes\"}")"
- check "bad since -> 422" "422" "$(code "$WK" POST /consolidations -d "{\"namespace_id\":\"$NS\",\"since\":\"last week\"}")"
- check "invalid JSON -> 400" "400" "$(code "$WK" POST /consolidations -d '{"namespace_id":')"
- check "empty body -> 400" "400" "$(code "$WK" POST /consolidations)"
- check "no auth -> 401" "401" "$(curl -s -o /dev/null -w '%{http_code}' -X POST -d '{}' "$BASE/consolidations")"
- check "wrong method -> 405" "405" "$(code "$WK" GET /consolidations)"
-
- # ---- propose ------------------------------------------------------------------------------------------------------------
- cons "$WK" "{\"namespace_id\":\"$NS\",\"propose\":true}" > "$TMP/p.json"
- check "propose -> 2 candidates" "2" "$(jq_ "d['candidates_created']" < "$TMP/p.json")"
- CP=$(jq_ "d['groups'][0]['candidate_id']" < "$TMP/p.json")
- R=$(body "$RK" GET /candidates/$CP)
- 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']")"
- 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'])")"
- 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'])")"
- 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")
- 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'])")"
- 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")
- cons "$WK" "{\"namespace_id\":\"$NS\",\"propose\":true}" > "$TMP/p2.json"
- check "two new experiences -> a new proposal" "1" "$(jq_ "d['candidates_created']" < "$TMP/p2.json")"
-
- # ---- review and reuse ------------------------------------------------------------------------------------------------
- check "proposer with verify cannot approve its own candidate -> 403" "403" "$(code "$WK" POST /candidates/$CP/review -d '{"decision":"accept","verify":true}')"
- R=$(body "$RK" POST /candidates/$CP/review -d '{"decision":"accept","verify":true}')
- MEM=$(echo "$R" | jq_ "(d.get('data') or d).get('memory_id') or (d.get('data') or d).get('resulting_memory_id') or ''")
- check "reviewer accepts -> memory created" "yes" "$([ -n "$MEM" ] && echo yes || echo no)"
- 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'")"
- 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'])")"
-
- # ---- UTF-8 ------------------------------------------------------------------------------------------------------------
- NSU=$(prov namespace "$TA" /ops/utf | field namespace_id); SU=$(session "$WK" "$NSU" "utf")
- for oc in failure success; do
- 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"
- body "$WK" POST /sessions/$SU/events --data-binary @"$TMP/ev.json" >/dev/null
- done
- 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'")"
-
- echo "passed: $PASS failed: $FAIL"
- for f in "${FAILED[@]}"; do echo " FAIL: $f"; done
- [ "$FAIL" -eq 0 ]
|