2026-11-16 12:40:03 +01:00
|
|
|
|
"""Checks of the web app, without calling claude: .venv/bin/python app/server/check.py [name]"""
|
|
|
|
|
|
import json
|
|
|
|
|
|
import os
|
|
|
|
|
|
import shutil
|
|
|
|
|
|
import socket
|
|
|
|
|
|
import sys
|
|
|
|
|
|
import tempfile
|
|
|
|
|
|
import threading
|
|
|
|
|
|
import time
|
|
|
|
|
|
import urllib.request
|
|
|
|
|
|
from pathlib import Path
|
|
|
|
|
|
from urllib.error import HTTPError
|
|
|
|
|
|
|
|
|
|
|
|
HERE = Path(__file__).resolve().parent
|
|
|
|
|
|
sys.path.insert(0, str(HERE))
|
|
|
|
|
|
import uvicorn # noqa: E402
|
|
|
|
|
|
import server # noqa: E402
|
|
|
|
|
|
import station # noqa: E402
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def temp_folder():
|
|
|
|
|
|
return Path(tempfile.mkdtemp())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def check_server():
|
|
|
|
|
|
# the real server on a free port: the guard (Host, declared JSON), a read
|
|
|
|
|
|
with socket.socket() as so:
|
|
|
|
|
|
so.bind(("127.0.0.1", 0))
|
|
|
|
|
|
port = so.getsockname()[1]
|
|
|
|
|
|
srv = uvicorn.Server(uvicorn.Config(server.app, host="127.0.0.1", port=port, log_level="error"))
|
|
|
|
|
|
threading.Thread(target=srv.run, daemon=True).start()
|
|
|
|
|
|
while not srv.started:
|
|
|
|
|
|
time.sleep(0.05)
|
|
|
|
|
|
base = f"http://127.0.0.1:{port}"
|
|
|
|
|
|
|
|
|
|
|
|
def call(path, body=None, headers=None, method=None):
|
|
|
|
|
|
r = urllib.request.Request(base + path, json.dumps(body).encode() if body is not None else None,
|
|
|
|
|
|
{"Content-Type": "application/json", **(headers or {})}, method=method)
|
|
|
|
|
|
try:
|
|
|
|
|
|
with urllib.request.urlopen(r) as rep:
|
|
|
|
|
|
return rep.status
|
|
|
|
|
|
except HTTPError as e:
|
|
|
|
|
|
return e.code
|
|
|
|
|
|
try:
|
|
|
|
|
|
assert call("/api/stations", headers={"Host": "evil.example:80"}) == 403
|
|
|
|
|
|
assert call("/api/labels", {"station": "x"}, {"Content-Type": "text/plain"}) == 403
|
|
|
|
|
|
assert call("/api/stations") == 200
|
|
|
|
|
|
assert call("/api/analyses") == 200
|
2026-11-18 07:17:51 +01:00
|
|
|
|
assert call("/api/support") == 200
|
2026-11-20 08:28:19 +01:00
|
|
|
|
assert call("/api/points") == 200
|
|
|
|
|
|
assert call("/api/points/..%2Fx") in (400, 404)
|
2026-11-18 07:17:51 +01:00
|
|
|
|
assert call("/api/support", {"station": "x"}) == 400
|
2026-11-16 12:40:03 +01:00
|
|
|
|
assert call("/api/analyses/UNKNOWN/optimize", {}) == 400
|
|
|
|
|
|
finally:
|
|
|
|
|
|
srv.should_exit = True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def refuses(f, *a):
|
|
|
|
|
|
try:
|
|
|
|
|
|
f(*a)
|
|
|
|
|
|
except station.Refusal as e:
|
|
|
|
|
|
return str(e)
|
|
|
|
|
|
raise AssertionError(f"{f.__name__}{a} did not refuse")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def check_station():
|
|
|
|
|
|
# the data of the Data page, on the real data; the labels in a temporary folder
|
|
|
|
|
|
assert "NIEDERHORN" in station.stations()
|
|
|
|
|
|
im = station.images("NIEDERHORN", "2026-09-25", "2026-09-26")
|
|
|
|
|
|
assert im["images"] and im["positions"] and set(im["corr"]) == set(im["positions"]), list(im)
|
|
|
|
|
|
assert all(len(c) == len(im["images"]) for c in im["corr"].values())
|
|
|
|
|
|
assert station.image("NIEDERHORN", im["images"][0]["name"]).is_file()
|
|
|
|
|
|
refuses(station.image, "NIEDERHORN", "../../../.env")
|
|
|
|
|
|
refuses(station.images, "../API", "", "")
|
|
|
|
|
|
assert station.images("LENZSPITZE", "2026-10-26", "2026-10-27")["images"]
|
|
|
|
|
|
c = station.curves("LENZSPITZE", "2026-10-26", "2026-10-27")
|
|
|
|
|
|
titles = [p["title"] for p in c["plots"]]
|
|
|
|
|
|
assert "Relative Corrected Vertical Shift" in titles and any(t.startswith("System: ") for t in titles), titles
|
|
|
|
|
|
ev = station.detectors("LENZSPITZE", "2026-10-26", "2026-10-27") # the detectors of the journal, ~10 s
|
|
|
|
|
|
assert ev and {"start", "end", "family", "description", "sensors"} <= set(ev[0]), ev[:1]
|
|
|
|
|
|
assert all(e["message"] and e["action"] and set(e["targets"] + e["references"]) == set(e["sensors"]) for e in ev), ev[:1]
|
|
|
|
|
|
assert all(e["end"] >= "2026-10-26T00:00Z" and e["start"] < "2026-10-27T00:00Z" for e in ev), [(e["start"], e["end"]) for e in ev]
|
2026-11-17 09:12:17 +01:00
|
|
|
|
refuses(station.pump, {"stations": ["A;rm"]}) # the pumping (extraction/pumping.py, checked there): a refusal
|
2026-11-19 11:13:38 +01:00
|
|
|
|
refuses(station.pump, {"stations": ["A;rm"], "diff": True})
|
|
|
|
|
|
assert set(station.pump_state(True)) == {"running", "log"}
|
2026-11-16 12:40:03 +01:00
|
|
|
|
assert not any("SIM_CCID" in t or t.startswith("System: ip") for t in titles)
|
|
|
|
|
|
assert all(len(c["T"][tr["t"]]) == len(tr["y"]) for p in c["plots"] for tr in p["traces"])
|
2026-11-19 11:01:00 +01:00
|
|
|
|
if run := next((s for s in station.stations() if station.RUN in s), None): # a run of data_diff/, read as a station
|
|
|
|
|
|
c = station.curves(run, "", "")
|
|
|
|
|
|
assert c["plots"] and station.images(run, c["A"], c["B"])["images"], run
|
|
|
|
|
|
assert station.label_file(run, "auto").name == station.serial(run) + ".jsonl"
|
|
|
|
|
|
assert "data_diff" in refuses(station.pumped, run)
|
2026-11-16 12:40:03 +01:00
|
|
|
|
before, station.LABELS = station.LABELS, temp_folder()
|
|
|
|
|
|
try:
|
|
|
|
|
|
e = station.add({"station": "NIEDERHORN", "folder": "auto", "start": "2026-09-25T01:00Z",
|
|
|
|
|
|
"end": "2026-09-25T02:00Z", "family": "Visibility", "description": "fog"})
|
|
|
|
|
|
assert e["id"] == "NIEDERHORN-1" and next(iter(e)) == "id", e
|
2026-11-17 09:32:20 +01:00
|
|
|
|
refuses(station.update, {"station": "NIEDERHORN", "folder": "auto", "before": e, # labeling/scripts/validate.py
|
|
|
|
|
|
"start": e["start"], "end": e["end"], "family": "", "description": "rain"})
|
2026-11-16 12:40:03 +01:00
|
|
|
|
e2 = station.update({"station": "NIEDERHORN", "folder": "auto", "before": e,
|
2026-11-17 09:32:20 +01:00
|
|
|
|
"start": e["start"], "end": e["end"], "family": "Obstruction", "description": "rain"})
|
|
|
|
|
|
assert e2 == {"id": "NIEDERHORN-1", "start": e["start"], "end": e["end"], "family": "Obstruction", "description": "rain"}, e2
|
2026-11-16 12:40:03 +01:00
|
|
|
|
assert station.read("NIEDERHORN", "auto") == [e2]
|
|
|
|
|
|
assert "no longer in the file" in refuses(station.delete, {"station": "NIEDERHORN", "folder": "auto", "before": e})
|
|
|
|
|
|
station.delete({"station": "NIEDERHORN", "folder": "auto", "before": e2})
|
|
|
|
|
|
assert station.read("NIEDERHORN", "auto") == []
|
|
|
|
|
|
refuses(station.add, {"station": "NIEDERHORN", "folder": "../x", "start": "a", "end": "b", "description": "x"})
|
2026-11-17 09:32:20 +01:00
|
|
|
|
refuses(station.add, {"station": "NIEDERHORN", "folder": "manual", "start": "2026-09-25T01:00Z", # the expert's
|
|
|
|
|
|
"end": "2026-09-25T02:00Z", "family": "Visibility", "description": "fog"})
|
|
|
|
|
|
refuses(station.add, {"station": "NIEDERHORN", "start": "2026-09-25T01:00Z", "end": "2026-09-25T02:00Z",
|
|
|
|
|
|
"family": "Visibility", "description": "a shift of 3 px"})
|
2026-11-16 12:40:03 +01:00
|
|
|
|
refuses(station.add, {"station": "NIEDERHORN", "start": "2026-09-25T02:00Z", "end": "2026-09-25T01:00Z", "description": "x"})
|
|
|
|
|
|
finally:
|
|
|
|
|
|
shutil.rmtree(station.LABELS)
|
|
|
|
|
|
station.LABELS = before
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def check_optimization():
|
|
|
|
|
|
# the Optimize button (analysis.optimize), without claude: a fake process that ends only on a signal, a finished
|
|
|
|
|
|
# analysis copied into a temporary folder; the lock (one optimization at a time, no analysis or message meanwhile), the
|
|
|
|
|
|
# state read again after a restart
|
|
|
|
|
|
import analysis
|
|
|
|
|
|
end = threading.Event()
|
|
|
|
|
|
|
|
|
|
|
|
launched = []
|
|
|
|
|
|
|
|
|
|
|
|
class FakeProcess:
|
|
|
|
|
|
returncode = 0
|
|
|
|
|
|
stdout = iter(lambda: end.wait() and "", "") # nothing, until the signal
|
|
|
|
|
|
|
|
|
|
|
|
def __init__(self, args, *a, **k):
|
|
|
|
|
|
launched.append((args, k))
|
|
|
|
|
|
|
|
|
|
|
|
def wait(self):
|
|
|
|
|
|
return 0
|
|
|
|
|
|
|
|
|
|
|
|
before = analysis.FOLDER, analysis.subprocess.Popen, analysis.OPTIMIZER, analysis.SESSIONS, analysis.AGENTS
|
|
|
|
|
|
key = os.environ.get("ANTHROPIC_API_KEY")
|
|
|
|
|
|
os.environ["ANTHROPIC_API_KEY"] = "test-key"
|
|
|
|
|
|
root = temp_folder()
|
|
|
|
|
|
analysis.FOLDER, analysis.OPTIMIZER, analysis.AGENTS = root / "analyses", root / "optimizer", root / "agents"
|
|
|
|
|
|
analysis.SESSIONS = analysis.OPTIMIZER / "sessions"
|
|
|
|
|
|
analysis.subprocess.Popen = FakeProcess
|
|
|
|
|
|
try:
|
|
|
|
|
|
for name in ("NORDEND_a", "NORDEND_b"):
|
|
|
|
|
|
(analysis.FOLDER / name / "work").mkdir(parents=True)
|
|
|
|
|
|
(analysis.FOLDER / name / "request.json").write_text(json.dumps(
|
|
|
|
|
|
{"station": "NORDEND", "start": "2026-11-04T21:12Z", "end": "2026-11-07T14:12Z", "hints": ""}))
|
|
|
|
|
|
(analysis.FOLDER / name / "report.md").write_text("# report")
|
|
|
|
|
|
assert analysis.state("NORDEND_a")["optimization"] is None
|
|
|
|
|
|
# without Optimizer, then without agents: a clear refusal, not an error trace
|
|
|
|
|
|
assert "optimizer/" in refuses(analysis.optimize, "NORDEND_a")
|
2026-11-16 14:15:55 +01:00
|
|
|
|
(analysis.OPTIMIZER / ".claude" / "skills" / "optimize").mkdir(parents=True)
|
|
|
|
|
|
(analysis.OPTIMIZER / ".claude" / "skills" / "optimize" / "SKILL.md").write_text("optimize")
|
2026-11-16 12:40:03 +01:00
|
|
|
|
assert "agents/" in refuses(analysis.optimize, "NORDEND_a")
|
|
|
|
|
|
(analysis.AGENTS / ".claude-plugin").mkdir(parents=True)
|
|
|
|
|
|
(analysis.AGENTS / ".claude-plugin" / "plugin.json").write_text("{}")
|
2026-11-16 14:15:55 +01:00
|
|
|
|
assert "optimizer/" in refuses(analysis.optimize, "NORDEND_a") # without the links of Optimizer
|
2026-11-16 12:40:03 +01:00
|
|
|
|
for link in ("data", "labeling", "detectors"):
|
|
|
|
|
|
(analysis.OPTIMIZER / link).mkdir()
|
2026-11-16 14:15:55 +01:00
|
|
|
|
assert "optimizer/" in refuses(analysis.optimize, "NORDEND_a") # without its tools link
|
|
|
|
|
|
(analysis.OPTIMIZER / "tools").mkdir()
|
2026-11-16 12:40:03 +01:00
|
|
|
|
assert analysis.optimize("NORDEND_a") == {}
|
|
|
|
|
|
assert analysis.state("NORDEND_a")["optimization"]["state"] == "running"
|
|
|
|
|
|
refuses(analysis.optimize, "NORDEND_a")
|
|
|
|
|
|
refuses(analysis.optimize, "NORDEND_b")
|
|
|
|
|
|
refuses(analysis.launch, {"station": "NORDEND", "start": "2026-11-04T00:00Z", "end": "2026-11-05T00:00Z"})
|
|
|
|
|
|
refuses(analysis.message, "NORDEND_b", {"text": "continue"})
|
|
|
|
|
|
end.set()
|
|
|
|
|
|
while analysis.state("NORDEND_a")["optimization"]["state"] == "running":
|
|
|
|
|
|
time.sleep(0.05)
|
|
|
|
|
|
assert analysis.state("NORDEND_a")["optimization"]["state"] == "done"
|
2026-11-16 14:15:55 +01:00
|
|
|
|
assert len(launched) == 1, launched # a single call: /optimize in Optimizer, on the analysis
|
|
|
|
|
|
args, k = launched[0]
|
2026-11-16 12:40:03 +01:00
|
|
|
|
request = args[2]
|
2026-11-16 14:15:55 +01:00
|
|
|
|
assert request.startswith("/optimize ANALYSIS: labeling/auto/analyses/NORDEND_a/. OUT: sessions/NORDEND_a_") \
|
|
|
|
|
|
and "Caller's context:" in request, request
|
2026-11-16 12:40:03 +01:00
|
|
|
|
assert k["cwd"] == analysis.OPTIMIZER.resolve() and "--plugin-dir" in args, (args, k)
|
|
|
|
|
|
assert all("ANTHROPIC_API_KEY" not in kw["env"] for _, kw in launched), "an API key reaches claude"
|
|
|
|
|
|
# isolated from the settings of the workstation (the user's plugins, hooks, MCP servers)
|
|
|
|
|
|
assert all(a[a.index("--setting-sources") + 1] == "project,local" and "--strict-mcp-config" in a for a, _ in launched)
|
|
|
|
|
|
analysis.analyses["NORDEND_b"]["state"] = "running" # a running analysis blocks the optimization
|
|
|
|
|
|
refuses(analysis.optimize, "NORDEND_a")
|
|
|
|
|
|
# after a restart: the stream ends with {"type": "end"} -> done; without that line -> interrupted; the summary of
|
|
|
|
|
|
# the detectors and that of the tools in one page
|
|
|
|
|
|
stream = analysis.FOLDER / "NORDEND_a" / "work" / "optimization.jsonl"
|
|
|
|
|
|
assert stream.read_text().endswith('{"type": "end", "state": "done"}\n'), stream.read_text()
|
|
|
|
|
|
analysis.analyses.clear()
|
|
|
|
|
|
analysis.optimizations.clear()
|
|
|
|
|
|
assert analysis.state("NORDEND_a")["optimization"]["state"] == "done"
|
|
|
|
|
|
stream.write_text('{"type": "assistant", "message": {"content": []}}\n')
|
|
|
|
|
|
analysis.optimizations.clear()
|
|
|
|
|
|
assert analysis.state("NORDEND_a")["optimization"]["state"] == "interrupted"
|
|
|
|
|
|
(analysis.SESSIONS / "NORDEND_a_20261113T1000Z").mkdir(parents=True)
|
2026-11-16 14:15:55 +01:00
|
|
|
|
(analysis.SESSIONS / "NORDEND_a_20261113T1000Z" / "summary.md").write_text("# optimize\n\n**Result**: none")
|
2026-11-16 12:40:03 +01:00
|
|
|
|
# a new analysis of the same range (NORDEND_a_143000): its session is not the summary of NORDEND_a
|
|
|
|
|
|
(analysis.SESSIONS / "NORDEND_a_143000_20261113T1100Z").mkdir(parents=True)
|
|
|
|
|
|
(analysis.SESSIONS / "NORDEND_a_143000_20261113T1100Z" / "summary.md").write_text("# other analysis")
|
|
|
|
|
|
assert analysis._summary_file("NORDEND_a").parent.name == "NORDEND_a_20261113T1000Z", analysis._summary_file("NORDEND_a")
|
|
|
|
|
|
(analysis.FOLDER / "NORDEND_a" / "tools.md").write_text("# Tools\n\n**Tools changed**: none")
|
|
|
|
|
|
assert analysis.state("NORDEND_a")["optimization"]["summary"]
|
|
|
|
|
|
page = analysis.summary("NORDEND_a", False)
|
|
|
|
|
|
assert "<strong>Result</strong>" in page and "<strong>Tools changed</strong>" in page, page
|
|
|
|
|
|
assert analysis.state("NORDEND_b")["optimization"] is None
|
|
|
|
|
|
finally:
|
|
|
|
|
|
end.set()
|
|
|
|
|
|
os.environ.pop("ANTHROPIC_API_KEY")
|
|
|
|
|
|
if key is not None:
|
|
|
|
|
|
os.environ["ANTHROPIC_API_KEY"] = key
|
|
|
|
|
|
shutil.rmtree(root)
|
|
|
|
|
|
analysis.FOLDER, analysis.subprocess.Popen, analysis.OPTIMIZER, analysis.SESSIONS, analysis.AGENTS = before
|
|
|
|
|
|
analysis.analyses.clear()
|
|
|
|
|
|
analysis.optimizations.clear()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def check_analysis():
|
|
|
|
|
|
# "Analyze the range" then a message (analysis.launch, analysis.message), without claude: the analyst of the agents
|
|
|
|
|
|
# repository, its parameters, and the resumption of its session with the same agent
|
|
|
|
|
|
import analysis
|
|
|
|
|
|
launched = []
|
|
|
|
|
|
|
|
|
|
|
|
class FakeProcess:
|
|
|
|
|
|
returncode = 0
|
|
|
|
|
|
|
|
|
|
|
|
def __init__(self, args, *a, **k):
|
|
|
|
|
|
launched.append((args, k))
|
|
|
|
|
|
self.stdout = iter([])
|
|
|
|
|
|
|
|
|
|
|
|
def wait(self):
|
|
|
|
|
|
return 0
|
|
|
|
|
|
|
|
|
|
|
|
(station.ROOT / "out").mkdir(exist_ok=True) # under the root: the request cites the output folder relative to it
|
|
|
|
|
|
before = analysis.FOLDER, analysis.subprocess.Popen, analysis.submit
|
|
|
|
|
|
analysis.FOLDER = Path(tempfile.mkdtemp(dir=station.ROOT / "out"))
|
|
|
|
|
|
analysis.subprocess.Popen = FakeProcess
|
|
|
|
|
|
analysis.submit = lambda *a: 0 # otherwise the test rewrites the real labeling/auto/NORDEND.jsonl
|
|
|
|
|
|
key = os.environ.get("ANTHROPIC_API_KEY")
|
|
|
|
|
|
os.environ["ANTHROPIC_API_KEY"] = "test-key" # the agent runs on the subscription, never on an API key
|
|
|
|
|
|
try:
|
|
|
|
|
|
name = analysis.launch({"station": "NORDEND", "start": "2026-11-05T06:00Z", "end": "2026-11-05T12:00Z"})["name"]
|
|
|
|
|
|
while analysis.analyses[name]["state"] == "running":
|
|
|
|
|
|
time.sleep(0.05)
|
|
|
|
|
|
assert analysis.analyses[name]["state"] == "done", analysis.analyses[name]
|
|
|
|
|
|
assert json.loads((analysis.FOLDER / name / "request.json").read_text()) == \
|
|
|
|
|
|
{"station": "NORDEND", "start": "2026-11-05T06:00Z", "end": "2026-11-05T12:00Z", "hints": ""}
|
|
|
|
|
|
args, k = launched[0]
|
|
|
|
|
|
assert args[args.index("--agent") + 1] == "agents:analyst" and "--plugin-dir" in args, args
|
|
|
|
|
|
assert "ANTHROPIC_API_KEY" not in k["env"] and "PATH" in k["env"], sorted(k.get("env") or {})
|
|
|
|
|
|
assert args[2].startswith("Station: data/NORDEND/. Range: 2026-11-05T06:00Z → 2026-11-05T12:00Z (UTC). Hints: none. ") \
|
|
|
|
|
|
and "DETECTORS: no" in args[2] and "INSTRUCTIONS: instructions/analyst.md" in args[2], args[2]
|
|
|
|
|
|
assert args[args.index("--setting-sources") + 1] == "project,local" and "--strict-mcp-config" in args, args
|
|
|
|
|
|
(analysis.FOLDER / name / "work" / "session").write_text("s-1")
|
|
|
|
|
|
analysis.message(name, {"text": "continue"})
|
|
|
|
|
|
while analysis.analyses[name]["state"] == "running":
|
|
|
|
|
|
time.sleep(0.05)
|
|
|
|
|
|
args, k = launched[1]
|
|
|
|
|
|
assert "ANTHROPIC_API_KEY" not in k["env"], sorted(k.get("env") or {})
|
|
|
|
|
|
assert args[args.index("--resume") + 1] == "s-1" and args[args.index("--agent") + 1] == "agents:analyst" \
|
|
|
|
|
|
and "--plugin-dir" in args, args
|
|
|
|
|
|
agents, analysis.AGENTS = analysis.AGENTS, analysis.FOLDER / "no-agents" # the agents/ link is missing: clear refusal
|
|
|
|
|
|
try:
|
|
|
|
|
|
assert "agents/" in refuses(analysis.launch, {"station": "NORDEND", "start": "2026-11-06T06:00Z", "end": "2026-11-06T12:00Z"})
|
|
|
|
|
|
finally:
|
|
|
|
|
|
analysis.AGENTS = agents
|
|
|
|
|
|
finally:
|
|
|
|
|
|
os.environ.pop("ANTHROPIC_API_KEY")
|
|
|
|
|
|
if key is not None:
|
|
|
|
|
|
os.environ["ANTHROPIC_API_KEY"] = key
|
|
|
|
|
|
shutil.rmtree(analysis.FOLDER)
|
|
|
|
|
|
analysis.FOLDER, analysis.subprocess.Popen, analysis.submit = before
|
|
|
|
|
|
analysis.analyses.clear()
|
|
|
|
|
|
|
|
|
|
|
|
|
2026-11-17 10:12:14 +01:00
|
|
|
|
def check_validation():
|
2026-11-17 12:01:23 +01:00
|
|
|
|
# "Validate labels" (analysis.validate): review on all the labels of the analysis, in its labels.jsonl (which says
|
|
|
|
|
|
# whether it is validated) and submitted with their id; the others untouched; an edit in the page removes it from one
|
|
|
|
|
|
# label, there too, and the analysis is no longer validated
|
2026-11-17 10:12:14 +01:00
|
|
|
|
import analysis
|
|
|
|
|
|
before = station.LABELS, analysis.FOLDER
|
|
|
|
|
|
station.LABELS = temp_folder()
|
|
|
|
|
|
analysis.FOLDER = station.LABELS / "auto" / "analyses"
|
|
|
|
|
|
try:
|
|
|
|
|
|
for name, report in (("NORDEND_a", True), ("NORDEND_b", False)):
|
2026-11-17 12:01:23 +01:00
|
|
|
|
(analysis.FOLDER / name / "work").mkdir(parents=True)
|
2026-11-17 10:12:14 +01:00
|
|
|
|
(analysis.FOLDER / name / "request.json").write_text(json.dumps({"station": "NORDEND"}))
|
|
|
|
|
|
if report:
|
|
|
|
|
|
(analysis.FOLDER / name / "report.md").write_text("# report")
|
2026-11-17 12:01:23 +01:00
|
|
|
|
mine = [{"start": "2026-11-05T06:00Z", "end": "2026-11-05T07:00Z", "family": "Visibility",
|
|
|
|
|
|
"description": f"fog {i}"} for i in (1, 2)]
|
2026-11-17 10:12:14 +01:00
|
|
|
|
other = {"id": "NORDEND-3", "start": "2026-11-05T06:00Z", "end": "2026-11-05T07:00Z", "family": "Visibility",
|
|
|
|
|
|
"description": "rain"}
|
2026-11-17 12:01:23 +01:00
|
|
|
|
station.write(analysis.FOLDER / "NORDEND_a" / "labels.jsonl", mine)
|
|
|
|
|
|
station.write(station.LABELS / "auto" / "NORDEND.jsonl",
|
|
|
|
|
|
[{"id": f"NORDEND-{i}"} | e | {"analysis": "NORDEND_a"} for i, e in enumerate(mine, 1)] + [other])
|
|
|
|
|
|
analysis.state("NORDEND_a")
|
2026-11-17 10:12:14 +01:00
|
|
|
|
assert analysis.state("NORDEND_a")["validated"] is None
|
|
|
|
|
|
assert "finished" in refuses(analysis.validate, "NORDEND_b") # interrupted, no report
|
|
|
|
|
|
assert analysis.validate("NORDEND_a") == {"validated": 2}
|
2026-11-17 12:01:23 +01:00
|
|
|
|
theirs = analysis._labels(analysis.FOLDER / "NORDEND_a")
|
|
|
|
|
|
assert all(e.get("review") for e in theirs), theirs
|
2026-11-17 10:12:14 +01:00
|
|
|
|
labels = station.read("NORDEND", "auto")
|
2026-11-17 12:01:23 +01:00
|
|
|
|
assert {e["id"]: bool(e.get("review")) for e in labels} == \
|
|
|
|
|
|
{"NORDEND-1": True, "NORDEND-2": True, "NORDEND-3": False}, labels # the ids kept
|
|
|
|
|
|
assert analysis.state("NORDEND_a")["validated"] == theirs[0]["review"]
|
|
|
|
|
|
assert {a["name"]: a["validated"] for a in analysis.list_analyses()} == \
|
|
|
|
|
|
{"NORDEND_a": theirs[0]["review"], "NORDEND_b": None} # the sidebar
|
|
|
|
|
|
edited = next(e for e in labels if e["id"] == "NORDEND-1")
|
|
|
|
|
|
station.update({"station": "NORDEND", "before": edited, "family": "Visibility", "description": "fog again"})
|
2026-11-17 10:12:14 +01:00
|
|
|
|
assert analysis.state("NORDEND_a")["validated"] is None
|
2026-11-17 12:01:23 +01:00
|
|
|
|
assert [e["description"] for e in analysis._labels(analysis.FOLDER / "NORDEND_a")] == ["fog again", "fog 2"]
|
|
|
|
|
|
assert analysis.validate("NORDEND_a") == {"validated": 2} # validated again, the edit and the ids kept
|
|
|
|
|
|
assert sorted((e["id"], e["description"]) for e in station.read("NORDEND", "auto")) == \
|
|
|
|
|
|
[("NORDEND-1", "fog again"), ("NORDEND-2", "fog 2"), ("NORDEND-3", "rain")]
|
|
|
|
|
|
station.write(analysis.FOLDER / "NORDEND_a" / "labels.jsonl", [])
|
2026-11-17 10:12:14 +01:00
|
|
|
|
assert "no label" in refuses(analysis.validate, "NORDEND_a")
|
|
|
|
|
|
finally:
|
|
|
|
|
|
shutil.rmtree(station.LABELS)
|
|
|
|
|
|
station.LABELS, analysis.FOLDER = before
|
|
|
|
|
|
analysis.analyses.clear()
|
|
|
|
|
|
|
|
|
|
|
|
|
2026-11-18 07:17:51 +01:00
|
|
|
|
def check_support():
|
|
|
|
|
|
# "Ask the support" then a message (support.launch, support.message), without claude: the support of the agents
|
|
|
|
|
|
# repository, its request (the question, its instructions, its absolute folder), the long commands it is given, the
|
|
|
|
|
|
# resumption of its session, its answer in the page; the state read again after a restart
|
|
|
|
|
|
import analysis
|
|
|
|
|
|
import support
|
|
|
|
|
|
launched = []
|
|
|
|
|
|
|
|
|
|
|
|
class FakeProcess:
|
|
|
|
|
|
returncode = 0
|
|
|
|
|
|
|
|
|
|
|
|
def __init__(self, args, *a, **k):
|
|
|
|
|
|
launched.append((args, k))
|
|
|
|
|
|
self.stdout = iter(['{"type":"system","subtype":"init","session_id":"s-1"}\n'])
|
|
|
|
|
|
|
|
|
|
|
|
def wait(self):
|
|
|
|
|
|
return 0
|
|
|
|
|
|
|
|
|
|
|
|
root = temp_folder()
|
|
|
|
|
|
before = support.FOLDER, analysis.subprocess.Popen, analysis.AGENTS
|
|
|
|
|
|
support.FOLDER, analysis.AGENTS, analysis.subprocess.Popen = root / "support", root / "agents", FakeProcess
|
|
|
|
|
|
key = os.environ.get("ANTHROPIC_API_KEY")
|
|
|
|
|
|
os.environ["ANTHROPIC_API_KEY"] = "test-key"
|
|
|
|
|
|
ask = {"station": "NORDEND", "start": "2026-11-05T06:00Z", "end": "2026-11-05T12:00Z", "question": "Why the jump?"}
|
|
|
|
|
|
try:
|
|
|
|
|
|
assert "agents/" in refuses(support.launch, ask)
|
|
|
|
|
|
(analysis.AGENTS / "agents").mkdir(parents=True)
|
|
|
|
|
|
(analysis.AGENTS / "agents" / "support.md").write_text("support")
|
|
|
|
|
|
assert "question" in refuses(support.launch, ask | {"question": " "})
|
|
|
|
|
|
name = support.launch(ask)["name"]
|
|
|
|
|
|
while support.supports[name]["state"] == "running":
|
|
|
|
|
|
time.sleep(0.05)
|
|
|
|
|
|
assert support.state(name)["state"] == "done" and support.state(name)["report"] is None, support.state(name)
|
|
|
|
|
|
args, k = launched[0]
|
|
|
|
|
|
assert args[args.index("--agent") + 1] == "agents:support" and "Edit" in args[args.index("--allowedTools") + 1], args
|
|
|
|
|
|
assert "ANTHROPIC_API_KEY" not in k["env"] and k["env"]["BASH_MAX_TIMEOUT_MS"] == "10800000", sorted(k["env"])
|
|
|
|
|
|
out = support.FOLDER / name
|
|
|
|
|
|
assert f"Output folder: {out}/." in args[2] and f"INSTRUCTIONS: {support.INSTRUCTIONS}." in args[2] \
|
|
|
|
|
|
and args[2].endswith("Question: Why the jump?") and support.INSTRUCTIONS.is_file(), args[2]
|
|
|
|
|
|
assert (out / "work" / "session").read_text() == "s-1"
|
|
|
|
|
|
assert support.launch(ask)["name"].startswith(name + "_") # another question on the same range, next to it
|
|
|
|
|
|
(out / "answer.md").write_text("# NORDEND: a jump\n\n")
|
|
|
|
|
|
(out / "fig.png").write_bytes(b"png")
|
|
|
|
|
|
assert support.state(name)["report"] and "<h1>NORDEND: a jump</h1>" in support.answer(name, False)
|
|
|
|
|
|
assert analysis.file(name, "fig.png", support.FOLDER).is_file()
|
|
|
|
|
|
refuses(analysis.file, name, "../../agents/agents/support.md", support.FOLDER)
|
|
|
|
|
|
while any(s["state"] == "running" for s in support.supports.values()):
|
|
|
|
|
|
time.sleep(0.05)
|
|
|
|
|
|
support.message(name, {"text": "And the night before?"})
|
|
|
|
|
|
while support.supports[name]["state"] == "running":
|
|
|
|
|
|
time.sleep(0.05)
|
|
|
|
|
|
args = launched[-1][0]
|
|
|
|
|
|
assert args[args.index("--resume") + 1] == "s-1" and args[2].startswith("And the night before?"), args
|
|
|
|
|
|
support.supports.clear() # after a restart: the thread read again in the log, done with its answer
|
|
|
|
|
|
s = support.state(name)
|
|
|
|
|
|
assert s["state"] == "done" and s["lines"] == ["› And the night before?"], s
|
|
|
|
|
|
assert {x["name"]: x["state"] for x in support.list_supports()}[name] == "done" # the sidebar
|
|
|
|
|
|
refuses(support.state, "../analyses")
|
|
|
|
|
|
finally:
|
|
|
|
|
|
os.environ.pop("ANTHROPIC_API_KEY")
|
|
|
|
|
|
if key is not None:
|
|
|
|
|
|
os.environ["ANTHROPIC_API_KEY"] = key
|
|
|
|
|
|
shutil.rmtree(root)
|
|
|
|
|
|
support.FOLDER, analysis.subprocess.Popen, analysis.AGENTS = before
|
|
|
|
|
|
support.supports.clear()
|
|
|
|
|
|
|
|
|
|
|
|
|
2026-11-18 15:10:48 +01:00
|
|
|
|
def check_conversations():
|
|
|
|
|
|
# the Conversations page (conversations.py): the list, the transcript rendered safely, the files, the pumping refused
|
|
|
|
|
|
# on a bad range; without LibreDesk (the pumping is libredesk/conversations/, tested there)
|
|
|
|
|
|
import conversations
|
|
|
|
|
|
root, before = temp_folder(), conversations.FOLDER
|
|
|
|
|
|
conversations.FOLDER = root
|
|
|
|
|
|
try:
|
|
|
|
|
|
c = root / "42"
|
|
|
|
|
|
(c / "attachments").mkdir(parents=True)
|
|
|
|
|
|
(c / "conversation.json").write_text(json.dumps({"reference_number": "42", "subject": "A jump", "status": "Open",
|
|
|
|
|
|
"updated_at": "2026-11-18T09:00:00Z", "contact": {"first_name": "Jean", "last_name": "", "email": "j@x.ch"}}))
|
|
|
|
|
|
(c / "messages.json").write_text(json.dumps([
|
|
|
|
|
|
{"type": "incoming", "created_at": "2026-11-18T08:00:00Z", "attachments": [{"url": "attachments/p.png"}]},
|
|
|
|
|
|
{"type": "activity", "created_at": "2026-11-18T08:05:00Z", "attachments": []},
|
|
|
|
|
|
{"type": "outgoing", "created_at": "2026-11-18T08:10:00Z", "attachments": [{"url": "attachments/x.svg"}]}]))
|
|
|
|
|
|
(c / "transcript.md").write_text("# A jump\n\n<script>alert(1)</script>\n\n\n")
|
|
|
|
|
|
(c / "attachments" / "p.png").write_bytes(b"png")
|
|
|
|
|
|
(c / "attachments" / "x.svg").write_text("<svg onload='alert(1)'/>")
|
|
|
|
|
|
(root / "pumped.json").write_text("{}") # not a conversation
|
|
|
|
|
|
assert conversations.list_conversations() == [{"reference": "42", "subject": "A jump", "contact": "Jean",
|
|
|
|
|
|
"status": "Open", "last": "2026-11-18T08:10:00Z", "messages": 2, "attachments": 2}], conversations.list_conversations()
|
|
|
|
|
|
page = conversations.transcript("42", False)
|
|
|
|
|
|
assert "<script>" not in page and "<script>" in page and '<img alt="p" src="attachments/p.png"' in page, page
|
|
|
|
|
|
assert server.conversation_file("42", "attachments/x.svg").headers["content-disposition"].startswith("attachment")
|
|
|
|
|
|
png = server.conversation_file("42", "attachments/p.png").headers
|
|
|
|
|
|
assert "content-disposition" not in png and png["x-content-type-options"] == "nosniff"
|
|
|
|
|
|
assert "img-src 'self'" in server.conversation_transcript("42").headers["content-security-policy"]
|
|
|
|
|
|
refuses(conversations.transcript, "../analyses", False)
|
|
|
|
|
|
refuses(server.conversation_file, "42", "../../etc/passwd")
|
2026-11-18 15:28:17 +01:00
|
|
|
|
if conversations.pompage: # ~/aimsight-libredesk may be on a branch without conversations/ yet
|
|
|
|
|
|
assert "From and To" in refuses(conversations.pump, {"start": "2026-11-01"})
|
2026-11-18 15:10:48 +01:00
|
|
|
|
assert set(conversations.pump_state()) == {"running", "log"}
|
2026-11-18 15:24:23 +01:00
|
|
|
|
pompage, conversations.pompage = conversations.pompage, None # no libredesk/ link: the app still runs
|
|
|
|
|
|
try:
|
|
|
|
|
|
assert "libredesk/" in refuses(conversations.pump, {})
|
|
|
|
|
|
assert conversations.pump_state()["running"] is False and "libredesk/" in conversations.pump_state()["log"][0]
|
|
|
|
|
|
finally:
|
|
|
|
|
|
conversations.pompage = pompage
|
2026-11-18 15:10:48 +01:00
|
|
|
|
finally:
|
|
|
|
|
|
conversations.FOLDER = before
|
|
|
|
|
|
shutil.rmtree(root)
|
|
|
|
|
|
|
|
|
|
|
|
|
2026-11-20 08:28:19 +01:00
|
|
|
|
def check_points():
|
|
|
|
|
|
# the Points page: a dataset of the real data in a temporary folder; its format and its rules, labeling/scripts/points.py --test
|
|
|
|
|
|
import validity
|
|
|
|
|
|
before, validity.points.FOLDER = validity.points.FOLDER, str(temp_folder())
|
|
|
|
|
|
try:
|
|
|
|
|
|
n = validity.create({"station": "NIEDERHORN", "start": "2026-09-25", "end": "2026-09-26"})
|
|
|
|
|
|
assert n["points"] and not n["invalid"] and validity.listing("NIEDERHORN") == [n], n
|
|
|
|
|
|
d = validity.dataset(n["name"])
|
|
|
|
|
|
s, o = next(iter(d["sensors"].items()))
|
|
|
|
|
|
assert validity.mark(n["name"], {"sensor": s, "times": o["t"][:2], "valid": False})["invalid"] == 2
|
|
|
|
|
|
refuses(validity.create, {"station": "NIEDERHORN", "start": "2026-09-25", "end": "2026-09-26"}) # exists
|
2026-11-20 08:31:18 +01:00
|
|
|
|
refuses(validity.create, {"station": "NIEDERHORN@nope", "start": "2026-09-25", "end": "2026-09-26"}) # unknown run
|
2026-11-20 08:28:19 +01:00
|
|
|
|
refuses(validity.dataset, "../x")
|
|
|
|
|
|
refuses(validity.mark, n["name"], {"sensor": s, "times": ["2026-09-27 00:00:00"], "valid": False})
|
|
|
|
|
|
finally:
|
|
|
|
|
|
shutil.rmtree(validity.points.FOLDER)
|
|
|
|
|
|
validity.points.FOLDER = before
|
|
|
|
|
|
|
|
|
|
|
|
|
2026-11-25 11:21:27 +01:00
|
|
|
|
def check_suggestions():
|
|
|
|
|
|
# the Suggestions page (suggestions.py): the support agent's rows read from Supabase Conversations, mocked here
|
|
|
|
|
|
import suggestions
|
|
|
|
|
|
saved = {k: os.environ.pop(k, None) for k in ("SUPABASE_CONVERSATIONS_URL", "SUPABASE_CONVERSATIONS_KEY")}
|
|
|
|
|
|
real_get = suggestions._get
|
|
|
|
|
|
try:
|
|
|
|
|
|
assert "SUPABASE_CONVERSATIONS" in refuses(suggestions.list_suggestions)
|
|
|
|
|
|
os.environ.update(SUPABASE_CONVERSATIONS_URL="https://x.supabase.co", SUPABASE_CONVERSATIONS_KEY="k")
|
|
|
|
|
|
asked = []
|
|
|
|
|
|
|
|
|
|
|
|
def fake(path):
|
|
|
|
|
|
asked.append(path)
|
|
|
|
|
|
if path.startswith("/rest/v1/suggestions"):
|
|
|
|
|
|
return json.dumps([{"id": 12, "conversation_uuid": "c1", "status": "ready", "reply": "Bonjour",
|
|
|
|
|
|
"figures": [{"path": "12/1.png", "caption": "glare"}]}]).encode()
|
|
|
|
|
|
if path.startswith("/rest/v1/conversations"):
|
|
|
|
|
|
return json.dumps([{"uuid": "c1", "reference_number": "151", "subject": "Crissier"}]).encode()
|
|
|
|
|
|
return b"png"
|
|
|
|
|
|
suggestions._get = fake
|
|
|
|
|
|
rows = suggestions.list_suggestions()
|
|
|
|
|
|
assert rows[0]["reference"] == "151" and rows[0]["subject"] == "Crissier" and rows[0]["reply"] == "Bonjour", rows
|
|
|
|
|
|
refuses(suggestions.figure, "../x")
|
|
|
|
|
|
refuses(suggestions.figure, "1/a.png")
|
|
|
|
|
|
assert suggestions.figure("12/1.png") == b"png" and asked[-1] == "/storage/v1/object/suggestions/12/1.png", asked
|
|
|
|
|
|
r = server.suggestion_figure("12/1.png")
|
|
|
|
|
|
assert r.media_type == "image/png" and r.headers["x-content-type-options"] == "nosniff"
|
|
|
|
|
|
finally:
|
|
|
|
|
|
suggestions._get = real_get
|
|
|
|
|
|
for k, v in saved.items():
|
|
|
|
|
|
os.environ.pop(k, None)
|
|
|
|
|
|
if v is not None:
|
|
|
|
|
|
os.environ[k] = v
|
|
|
|
|
|
|
|
|
|
|
|
|
2026-11-20 08:28:19 +01:00
|
|
|
|
CHECKS = [check_server, check_station, check_optimization, check_analysis, check_validation, check_support, check_conversations,
|
2026-11-25 11:21:27 +01:00
|
|
|
|
check_points, check_suggestions]
|
2026-11-16 12:40:03 +01:00
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
|
name = sys.argv[1] if len(sys.argv) > 1 else None
|
|
|
|
|
|
for f in CHECKS:
|
|
|
|
|
|
if name in (None, f.__name__):
|
|
|
|
|
|
f()
|
|
|
|
|
|
print("ok", f.__name__)
|