askrobots / dbbasic-object-server
Public
Runs live, versioned Python application objects on one VM, with a suite of apps: projects, notes, tasks, files and more.
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 | |
| 3 | The client used to write its own history row after the reply, |
| 4 | fire-and-forget. Three failures followed, all found in one evening of |
| 5 | live debugging: a turn whose page died before the write vanished |
| 6 | entirely (billed in ai_usage, absent from history); no row carried a |
| 7 | timestamp, so "what happened today" was unanswerable and a date-filtered |
| 8 | query reported zero turns that existed; and nothing grouped a |
| 9 | conversation, so askrobots' resume-a-session had no substrate to port |
| 10 | onto. |
| 11 | |
| 12 | Same posture as the cost write beside it: server-authoritative, because |
| 13 | a record the client is trusted to write is a record that sometimes does |
| 14 | not exist. |
| 15 | """ |
| 16 | |
| 17 | import json |
| 18 | import pathlib |
| 19 | |
| 20 | import pytest |
| 21 | |
| 22 | REPO = pathlib.Path(__file__).resolve().parents[1] |
| 23 | |
| 24 | |
| 25 | @pytest.fixture(scope="module") |
| 26 | def server_source(): |
| 27 | return (REPO / "object_server.py").read_text() |
| 28 | |
| 29 | |
| 30 | @pytest.fixture(scope="module") |
| 31 | def chat_section(server_source): |
| 32 | start = server_source.index("async def _handle_ai_chat") |
| 33 | return server_source[start:start + 24000] |
| 34 | |
| 35 | |
| 36 | def 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 | |
| 41 | def 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 | |
| 49 | def 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 | |
| 55 | def 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 | |
| 64 | def 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"]) |
| 74 | def 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 | |
| 80 | def 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"')]) |
| 89 | def 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 | |
| 96 | def 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 | |
| 106 | def 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 | |
| 115 | def 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 |