Runs live, versioned Python application objects on one VM, with a suite of apps: projects, notes, tasks, files and more.

0 downloads

dbbasic-object-server/tests/test_ai_turn_recording.py
Download zip
Clone
Clone with git
git clone https://gitstable.com/git/askrobots/dbbasic-object-server.git
HTTPS
https://gitstable.com/git/askrobots/dbbasic-object-server.git
SSH
ssh://git@gitstable.com:2222/askrobots/dbbasic-object-server.git

Cloning needs no sign-in. Pushing over HTTPS asks for your username and an access token. SSH needs your key under SSH keys.

danq9 signup: the person chooses the password and name on the confirm page 83c7aa6 2 weeks, 5 days ago History
test_ai_turn_recording.py · 5.1 KB · 123 lines
1"""Every AI turn is recorded by the server, stamped and session-grouped.
2
3The client used to write its own history row after the reply,
4fire-and-forget. Three failures followed, all found in one evening of
5live debugging: a turn whose page died before the write vanished
6entirely (billed in ai_usage, absent from history); no row carried a
7timestamp, so "what happened today" was unanswerable and a date-filtered
8query reported zero turns that existed; and nothing grouped a
9conversation, so askrobots' resume-a-session had no substrate to port
10onto.
11
12Same posture as the cost write beside it: server-authoritative, because
13a record the client is trusted to write is a record that sometimes does
14not exist.
15"""
16
17import json
18import pathlib
19
20import pytest
21
22REPO = pathlib.Path(__file__).resolve().parents[1]
23
24
25@pytest.fixture(scope="module")
26def server_source():
27 return (REPO / "object_server.py").read_text()
28
29
30@pytest.fixture(scope="module")
31def chat_section(server_source):
32 start = server_source.index("async def _handle_ai_chat")
33 return server_source[start:start + 24000]
34
35
36def test_the_turn_is_written_inside_the_chat_handler(chat_section):
37 assert '"shell_commands", turn_record' in chat_section
38 assert '"created_at": _utc_now_iso()' in chat_section
39
40
41def test_the_write_happens_before_the_reply_is_sent(chat_section):
42 """History must not depend on anything that happens after the
43 response starts -- that dependency is exactly the old bug."""
44 write_at = chat_section.index('"shell_commands", turn_record')
45 reply_at = chat_section.rindex("await _send_json")
46 assert write_at < reply_at
47
48
49def test_a_failed_history_write_never_fails_the_chat(chat_section):
50 """The reply matters more than the record of it."""
51 tail = chat_section[chat_section.index('"shell_commands", turn_record'):]
52 assert "except Exception:" in tail[:400]
53
54
55def test_the_turn_carries_its_evidence(chat_section):
56 """session_id groups it, source names the surface, duration_ms
57 answers 'why was that slow', tool_calls says what it actually did --
58 each one a question that was unanswerable from the old flat row."""
59 for field in ('"session_id"', '"source"', '"duration_ms"',
60 '"tool_calls"', '"model"'):
61 assert field in chat_section, field
62
63
64def test_the_schema_grew_the_same_fields():
65 schema = json.loads((REPO / "packages/app-shell/schemas/shell_commands.json")
66 .read_text())
67 names = {f["name"] for f in schema["fields"]}
68 assert {"session_id", "source", "model", "duration_ms",
69 "tool_calls", "created_at"} <= names
70 assert schema["version"] >= 3
71
72
73@pytest.mark.parametrize("page", ["talk", "shell"])
74def test_the_clients_no_longer_write_ai_history(page):
75 source = (REPO / f"packages/app-shell/objects/site/{page}.py").read_text()
76 assert 'record(input, replyText, "ai")' not in source
77 assert 'record(input, ok ? body.reply : body.error, "ai")' not in source
78
79
80def test_shell_still_records_its_non_ai_commands():
81 """Command turns (note/link/search) never touch the chat endpoint, so
82 the server cannot record them -- deleting record() wholesale would
83 have silently dropped that history."""
84 source = (REPO / "packages/app-shell/objects/site/shell.py").read_text()
85 assert 'record(input, "link saved", "link")' in source
86
87
88@pytest.mark.parametrize("page,source_tag", [("talk", '"talk"'), ("shell", '"shell"')])
89def test_the_clients_mint_and_send_a_session_id(page, source_tag):
90 source = (REPO / f"packages/app-shell/objects/site/{page}.py").read_text()
91 assert "let SESSION_ID = freshSessionId()" in source
92 assert "session_id: SESSION_ID" in source
93 assert f"source: {source_tag}" in source
94
95
96def test_talk_shows_a_moving_elapsed_counter_while_the_model_works():
97 """"we don't know because after 10 seconds..." -- a tool-using model
98 legitimately takes that long, and a frozen ellipsis for ten seconds
99 reads as dead. A counter that is moving is a page that is alive."""
100 source = (REPO / "packages/app-shell/objects/site/talk.py").read_text()
101 assert "startThinking()" in source
102 assert 'thinking\\\\u2026 " + s + "s"' in source
103 assert "stopThinking();" in source
104
105
106def test_talk_acknowledges_the_utterance_audibly():
107 """Voice-first: the ear should know the utterance fired without
108 looking at the screen. The blip reuses the audio context the iOS
109 unlock already opened -- no new permission, no new stream."""
110 source = (REPO / "packages/app-shell/objects/site/talk.py").read_text()
111 assert "function blip()" in source
112 assert "__dbbasicTalkUnlockCtx" in source[source.index("function blip()"):]
113
114
115def test_the_filtered_list_never_renders_a_uuid_as_a_row_title():
116 """The other half of tonight: "show me my notes" rendered seven
117 UUIDs, because renderFilteredList fell back title||name||ID. The
118 fallback now walks the record for the first human-shaped string and
119 skips id-shaped and date-shaped values BY SHAPE."""
120 source = (REPO / "packages/app-views/objects/site/view_render.py").read_text()
121 assert "r.title || r.name || r.id" not in source
122 assert "function rowLabel(r)" in source
123 assert "idish" in source