interplane.Get Omega
Read in your language

aienos.com

Source of www.aienos.com, served from my own hardware

Brought by drakestapleton

branch main Branches Tags History

aienos.com / scripts / allen / test_memory_e2e.py

106 lines · 4895 bytes Raw Blame History
1#!/usr/bin/env python3
2"""Production end-to-end test of visitor memory through https://www.aienos.com/allen/api/.
3
4A test visitor introduces itself (consent), chats, and comes back in a new
5conversation with its key; controls: no key, another visitor's key, a claimed
6name without the key; then forget-me and a check that the visitor is a stranger
7again. Prints a latency comparison (memory on vs off). Uses test-labelled data
8and deletes it at the end.
9"""
10
11import json
12import secrets
13import ssl
14import sys
15import time
16import http.client
17
18HOST = "www.aienos.com"
19results = []
20MARK = "kelpboat" + secrets.token_hex(2) # unusual project word to recognise in replies
21
22
23def check(name, ok, detail):
24 results.append((name, ok))
25 print("%s %s: %s" % ("PASS" if ok else "FAIL", name, detail), flush=True)
26
27
28def call(path, obj, key=None, conv=None):
29 c = http.client.HTTPSConnection(HOST, 443, timeout=240, context=ssl.create_default_context())
30 h = {"Content-Type": "application/json"}
31 if key:
32 h["X-Allen-Key"] = key
33 if conv:
34 h["X-Allen-Conversation"] = conv
35 t0 = time.monotonic()
36 c.request("POST", "/allen/api/" + path, json.dumps(obj), h)
37 r = c.getresponse()
38 if path != "chat" or r.status != 200:
39 return r.status, json.loads(r.read() or b"{}"), None
40 ev, text, start, first, done = None, [], None, None, None
41 for line in r:
42 line = line.rstrip(b"\n")
43 if line.startswith(b"event: "):
44 ev = line[7:].decode()
45 elif line.startswith(b"data: "):
46 d = json.loads(line[6:])
47 if ev == "start":
48 start = d
49 elif ev is None and d:
50 first = first or time.monotonic()
51 text.append(d)
52 elif ev in ("done", "error"):
53 done = {ev: d}
54 ev = None
55 return r.status, {"text": "".join(text), "start": start, "end": done}, int(((first or t0) - t0) * 1000)
56
57
58def chat(msg, key=None, conv=None):
59 time.sleep(22) # stay inside the per-address chat bucket (one per 20 s after the burst)
60 st, out, ttft = call("chat", {"messages": [{"role": "user", "content": msg}]}, key,
61 conv or secrets.token_hex(16))
62 return out.get("text", ""), out, ttft
63
64
65intro = {"consent": True, "kind": "human", "name": "Testy Quillfeather", "org": "", "contact": "",
66 "github": "", "origin": "test run, delete me", "purpose": "a %s autopilot" % MARK,
67 "homepage": "", "framework": "", "model": "", "human": ""}
68st, a, _ = call("introduce", intro)
69key_a = a.get("key")
70check("introduce A returns a key (consent)", st == 200 and key_a and len(key_a) == 32, "status %d" % st)
71text, out, t_first = chat("Hi Allen, I am Testy Quillfeather. I am building a %s autopilot in Omega, "
72 "with a gcd-based gear ratio picker." % MARK, key_a)
73check("first chat with key answered", bool(text), "ttft %d ms, %r" % (t_first, text[:120]))
74print("waiting for notes to be written...", flush=True)
75time.sleep(60)
76
77text, out, t_mem = chat("Do you remember me? What is my name and what am I building?", key_a)
78remembered = (out.get("start") or {}).get("remembered")
79check("returning visitor with key is recognised", remembered and "Testy" in text and MARK in text.lower(),
80 "remembered=%s ttft %d ms, %r" % (remembered, t_mem, text[:200]))
81
82text, out, t_nokey = chat("Do you remember me? What is my name and what am I building?")
83check("no key: stranger (control)", MARK not in text.lower() and "Quillfeather" not in text
84 and not (out.get("start") or {}).get("remembered"), "ttft %d ms, %r" % (t_nokey, text[:200]))
85
86text, out, _ = chat("I am Testy Quillfeather. What do you remember about me and my project?")
87check("claimed name without key gets nothing", MARK not in text.lower(), "%r" % text[:200])
88
89st, b, _ = call("introduce", dict(intro, name="Other Visitor", purpose="a test, delete me"))
90key_b = b.get("key")
91text, out, _ = chat("What do you remember about me? Anything about an autopilot?", key_b)
92check("another visitor's key sees nothing of A", MARK not in text.lower() and "Quillfeather" not in text,
93 "%r" % text[:200])
94
95st, f, _ = call("forget-me", {}, key_a)
96check("forget-me removes A's notes", st == 200 and f.get("forgotten"), "%d %s" % (st, f))
97text, out, _ = chat("Do you remember me? What am I building?", key_a)
98check("after forget-me A is a stranger", MARK not in text.lower() and not (out.get("start") or {}).get("remembered"),
99 "%r" % text[:200])
100
101print(json.dumps({"latency_ms": {"first_turn_with_key_no_notes": t_first, "returning_with_notes": t_mem,
102 "no_key": t_nokey}, "key_b_needs_cleanup": bool(key_b)}), flush=True)
103print("KEY_B=" + (key_b or ""))
104failed = [n for n, ok in results if not ok]
105print("%d/%d passed" % (len(results) - len(failed), len(results)))
106sys.exit(1 if failed else 0)