NeonClary Cursor commited on
Commit
beaa024
·
1 Parent(s): 2011737

test: probe BrainForge/Security via standard HANA path

Browse files

One-shot diagnostic that exercises /auth/login, /brainforge/get_models, /brainforge/get_personas, and /brainforge/get_inference using only the default HANA_USERNAME/HANA_PASSWORD - i.e. the same path every other Neon model uses - and writes per-step evidence to a debug log so we can confirm the 4090-x1-3 host now serves Security identically (no klatchat creds, no direct vLLM bypass).

Run with: docker compose exec app python /home/user/app/probe_security_via_standard_hana.py

Co-authored-by: Cursor <cursoragent@cursor.com>

backend/probe_security_via_standard_hana.py ADDED
@@ -0,0 +1,240 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """One-shot probe: does the standard HANA path now serve BrainForge/Security?
2
+
3
+ Intentionally bypasses the existing HanaClient (which still has klatchat
4
+ detection and direct-vLLM merging) and talks to HANA the same way we
5
+ talk to it for every OTHER Neon model:
6
+
7
+ POST {hana_base_url}/auth/login (with hana_username/hana_password only)
8
+ POST {hana_base_url}/brainforge/get_models
9
+ POST {hana_base_url}/brainforge/get_personas (for Security)
10
+ POST {hana_base_url}/brainforge/get_inference (for CybersecurityExpert persona)
11
+
12
+ Each step's outcome is appended as one NDJSON line to debug-896623.log
13
+ so we can confirm/reject H1-H5 from runtime evidence before changing
14
+ production code.
15
+
16
+ Run with:
17
+ docker compose exec app python probe_security_via_standard_hana.py
18
+ """
19
+ from __future__ import annotations
20
+
21
+ import json
22
+ import os
23
+ import time
24
+ from pathlib import Path
25
+
26
+ import httpx
27
+
28
+ LOG_PATH = Path("/home/user/app/debug-896623.log")
29
+ SESSION = "896623"
30
+ SECURITY_MODEL_ID = "BrainForge/Security@2026.03.18"
31
+
32
+
33
+ def _emit(hypothesis: str, message: str, data: dict) -> None:
34
+ """Append one NDJSON line to the debug log. Never raises."""
35
+ payload = {
36
+ "sessionId": SESSION,
37
+ "runId": "probe-security-standard",
38
+ "hypothesisId": hypothesis,
39
+ "location": "backend/probe_security_via_standard_hana.py",
40
+ "message": message,
41
+ "data": data,
42
+ "timestamp": int(time.time() * 1000),
43
+ }
44
+ try:
45
+ LOG_PATH.parent.mkdir(parents=True, exist_ok=True)
46
+ with LOG_PATH.open("a", encoding="utf-8") as f:
47
+ f.write(json.dumps(payload) + "\n")
48
+ except Exception as exc:
49
+ print(f"log emit failed: {exc!r}")
50
+
51
+
52
+ def main() -> None:
53
+ base = (os.environ.get("HANA_BASE_URL") or "https://hana.neonaialpha.com").rstrip("/")
54
+ user = os.environ.get("HANA_USERNAME") or "guest"
55
+ pwd = os.environ.get("HANA_PASSWORD") or ""
56
+
57
+ _emit(
58
+ "setup",
59
+ "starting probe with standard HANA creds only",
60
+ {
61
+ "hana_base_url": base,
62
+ "hana_username": user,
63
+ "hana_password_set": bool(pwd),
64
+ "hana_password_klatchat_set": bool(os.environ.get("HANA_KLATCHAT_PASSWORD") or os.environ.get("HANA_PASSWORD_KLATCHAT")),
65
+ "vllm_api_key_set": bool(os.environ.get("VLLM_API_KEY")),
66
+ },
67
+ )
68
+
69
+ with httpx.Client(timeout=60.0) as cli:
70
+ # 1) Standard auth.
71
+ try:
72
+ r = cli.post(
73
+ f"{base}/auth/login",
74
+ json={"username": user, "password": pwd, "token_name": "ProbeSecurity", "client_id": "llm-chat-tool"},
75
+ )
76
+ ok = 200 <= r.status_code < 300
77
+ data = r.json() if ok else {}
78
+ token = data.get("access_token", "")
79
+ _emit(
80
+ "auth",
81
+ "standard HANA /auth/login result",
82
+ {
83
+ "status": r.status_code,
84
+ "ok": ok,
85
+ "username_returned": data.get("username"),
86
+ "has_access_token": bool(token),
87
+ "expiration": data.get("expiration"),
88
+ },
89
+ )
90
+ if not ok or not token:
91
+ print("Auth failed; aborting probe.")
92
+ return
93
+ except Exception as exc:
94
+ _emit("auth", "standard HANA /auth/login raised", {"error": repr(exc)})
95
+ return
96
+
97
+ headers = {"Authorization": f"Bearer {token}", "Content-Type": "application/json"}
98
+
99
+ # 2) H1: does get_models include Security?
100
+ try:
101
+ r = cli.post(f"{base}/brainforge/get_models", headers=headers, json={})
102
+ payload = r.json() if 200 <= r.status_code < 300 else {}
103
+ models = payload.get("models", []) or []
104
+ mids = [f"{m.get('name')}@{m.get('version')}" for m in models]
105
+ security_entry = next(
106
+ (m for m in models if (str(m.get("name") or "").lower() == "brainforge/security")
107
+ or ("security" in str(m.get("name") or "").lower())),
108
+ None,
109
+ )
110
+ personas_in_security = []
111
+ if security_entry:
112
+ personas_in_security = [p.get("persona_name") for p in (security_entry.get("personas") or [])]
113
+ _emit(
114
+ "H1",
115
+ "standard /brainforge/get_models result",
116
+ {
117
+ "status": r.status_code,
118
+ "model_count": len(models),
119
+ "first_8_model_ids": mids[:8],
120
+ "all_model_ids_with_security": [m for m in mids if "security" in m.lower()],
121
+ "security_in_list": security_entry is not None,
122
+ "security_personas_in_get_models": personas_in_security,
123
+ },
124
+ )
125
+ except Exception as exc:
126
+ _emit("H1", "standard /brainforge/get_models raised", {"error": repr(exc)})
127
+
128
+ # 3) H2: does get_personas for Security return personas with prompts?
129
+ try:
130
+ r = cli.post(f"{base}/brainforge/get_personas", headers=headers, json={"model_id": SECURITY_MODEL_ID})
131
+ payload = r.json() if 200 <= r.status_code < 300 else {}
132
+ personas = payload.get("personas", []) or []
133
+ simplified = [
134
+ {
135
+ "persona_name": p.get("persona_name"),
136
+ "has_system_prompt": bool((p.get("system_prompt") or "").strip()),
137
+ "system_prompt_chars": len((p.get("system_prompt") or "").strip()),
138
+ "enabled": p.get("enabled", True),
139
+ }
140
+ for p in personas
141
+ ]
142
+ _emit(
143
+ "H2",
144
+ "standard /brainforge/get_personas for Security",
145
+ {
146
+ "status": r.status_code,
147
+ "model_id": SECURITY_MODEL_ID,
148
+ "persona_count": len(personas),
149
+ "personas": simplified,
150
+ "raw_body_preview": (r.text or "")[:400] if r.status_code != 200 else None,
151
+ },
152
+ )
153
+ cyber = next(
154
+ (p for p in personas if (p.get("persona_name") or "").lower() == "cybersecurityexpert"),
155
+ None,
156
+ )
157
+ except Exception as exc:
158
+ _emit("H2", "standard /brainforge/get_personas raised", {"error": repr(exc)})
159
+ cyber = None
160
+
161
+ # 4) H3: does get_inference work for CybersecurityExpert?
162
+ persona_name = (cyber or {}).get("persona_name") if cyber else "CybersecurityExpert"
163
+ system_prompt = (cyber or {}).get("system_prompt") or ""
164
+
165
+ def _call_inference(model_id: str, persona: str, sp: str, label: str, hyp: str) -> None:
166
+ persona_payload: dict = {"persona_name": persona}
167
+ if sp:
168
+ persona_payload["system_prompt"] = sp
169
+ body = {
170
+ "query": "In two short sentences, what is a zero-day vulnerability?" if "Security" in model_id
171
+ else "In one sentence, what is the capital of France?",
172
+ "history": [],
173
+ "persona": persona_payload,
174
+ "model": model_id,
175
+ "max_tokens": 120,
176
+ "temperature": 0.3,
177
+ "extra_body": {},
178
+ "llm_name": model_id.split("@")[0] if "@" in model_id else model_id,
179
+ "llm_revision": model_id.split("@")[1] if "@" in model_id else "",
180
+ }
181
+ try:
182
+ t0 = time.time()
183
+ r = cli.post(f"{base}/brainforge/get_inference", headers=headers, json=body)
184
+ elapsed_s = round(time.time() - t0, 2)
185
+ ok = 200 <= r.status_code < 300
186
+ resp_body = r.json() if ok else {}
187
+ response = (resp_body.get("response") or "").strip()
188
+ _emit(
189
+ hyp,
190
+ f"standard /brainforge/get_inference [{label}]",
191
+ {
192
+ "status": r.status_code,
193
+ "ok": ok,
194
+ "elapsed_seconds": elapsed_s,
195
+ "model_id": model_id,
196
+ "persona_used": persona,
197
+ "system_prompt_included": bool(sp),
198
+ "response_chars": len(response),
199
+ "response_preview": response[:240] if response else None,
200
+ "finish_reason": resp_body.get("finish_reason"),
201
+ "raw_body_preview": (r.text or "")[:300] if not ok else None,
202
+ },
203
+ )
204
+ except Exception as exc:
205
+ _emit(hyp, f"/brainforge/get_inference [{label}] raised", {"error": repr(exc)})
206
+
207
+ # Production-shaped call: persona_name + system_prompt (cached from get_personas).
208
+ _call_inference(SECURITY_MODEL_ID, persona_name, system_prompt, "Security/CybersecurityExpert", "H3")
209
+
210
+ # Control: another Neon model with the same payload shape.
211
+ # Pick the first non-Security model from get_models above. We refetch it cheaply.
212
+ try:
213
+ r = cli.post(f"{base}/brainforge/get_models", headers=headers, json={})
214
+ models2 = r.json().get("models", []) if 200 <= r.status_code < 300 else []
215
+ ctrl = None
216
+ for m in models2:
217
+ mid = f"{m.get('name')}@{m.get('version')}"
218
+ if "security" in mid.lower():
219
+ continue
220
+ # find any persona on this model
221
+ for p in (m.get("personas") or []):
222
+ pname = p.get("persona_name") or ""
223
+ if pname:
224
+ ctrl = (mid, pname, (p.get("system_prompt") or ""))
225
+ break
226
+ if ctrl:
227
+ break
228
+ if ctrl:
229
+ cmid, cpersona, csp = ctrl
230
+ _call_inference(cmid, cpersona, csp, f"control:{cmid}/{cpersona}", "H3-ctrl")
231
+ else:
232
+ _emit("H3-ctrl", "no control model found", {})
233
+ except Exception as exc:
234
+ _emit("H3-ctrl", "control fetch raised", {"error": repr(exc)})
235
+
236
+ _emit("done", "probe complete", {})
237
+
238
+
239
+ if __name__ == "__main__":
240
+ main()