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_object_ai.py · 28.6 KB · 779 lines
| 1 | """Tests for AI chat: provider adapters, the tool loop, and the endpoint.""" |
| 2 | |
| 3 | import json |
| 4 | |
| 5 | import pytest |
| 6 | |
| 7 | import object_ai |
| 8 | import object_mcp |
| 9 | import object_record_changes |
| 10 | import object_records |
| 11 | import object_server |
| 12 | |
| 13 | from test_object_server import ( |
| 14 | create_identity_session, |
| 15 | enable_admin_token, |
| 16 | request, |
| 17 | save_permission_policy, |
| 18 | write_records, |
| 19 | ) |
| 20 | |
| 21 | |
| 22 | def anthropic_text_response(text): |
| 23 | return ( |
| 24 | 200, |
| 25 | json.dumps( |
| 26 | { |
| 27 | "content": [{"type": "text", "text": text}], |
| 28 | "usage": {"input_tokens": 10, "output_tokens": 5}, |
| 29 | } |
| 30 | ).encode(), |
| 31 | ) |
| 32 | |
| 33 | |
| 34 | def anthropic_text_response_with_usage(text, *, input_tokens, output_tokens): |
| 35 | return ( |
| 36 | 200, |
| 37 | json.dumps( |
| 38 | { |
| 39 | "content": [{"type": "text", "text": text}], |
| 40 | "usage": {"input_tokens": input_tokens, "output_tokens": output_tokens}, |
| 41 | } |
| 42 | ).encode(), |
| 43 | ) |
| 44 | |
| 45 | |
| 46 | def openai_text_response_with_usage(text, *, prompt_tokens, completion_tokens): |
| 47 | return ( |
| 48 | 200, |
| 49 | json.dumps( |
| 50 | { |
| 51 | "choices": [{"message": {"role": "assistant", "content": text}}], |
| 52 | "usage": { |
| 53 | "prompt_tokens": prompt_tokens, |
| 54 | "completion_tokens": completion_tokens, |
| 55 | }, |
| 56 | } |
| 57 | ).encode(), |
| 58 | ) |
| 59 | |
| 60 | |
| 61 | def anthropic_tool_response(name, arguments): |
| 62 | return ( |
| 63 | 200, |
| 64 | json.dumps( |
| 65 | { |
| 66 | "content": [ |
| 67 | {"type": "tool_use", "id": "call-1", "name": name, "input": arguments} |
| 68 | ], |
| 69 | "usage": {"input_tokens": 20, "output_tokens": 8}, |
| 70 | } |
| 71 | ).encode(), |
| 72 | ) |
| 73 | |
| 74 | |
| 75 | def test_split_model_and_tool_conversion(): |
| 76 | assert object_ai.split_model("anthropic:claude-haiku-4-5") == ("anthropic", "claude-haiku-4-5") |
| 77 | with pytest.raises(object_ai.InvalidChatRequestError): |
| 78 | object_ai.split_model("claude-haiku-4-5") |
| 79 | with pytest.raises(object_ai.InvalidChatRequestError): |
| 80 | object_ai.split_model("mystery:model") |
| 81 | |
| 82 | tools = object_ai.mcp_tools_as_provider_tools( |
| 83 | ["global_search"], object_mcp.TOOLS, service="anthropic" |
| 84 | ) |
| 85 | assert tools[0]["name"] == "global_search" |
| 86 | assert "input_schema" in tools[0] |
| 87 | |
| 88 | openai_tools = object_ai.mcp_tools_as_provider_tools( |
| 89 | ["global_search"], object_mcp.TOOLS, service="openai" |
| 90 | ) |
| 91 | assert openai_tools[0]["function"]["name"] == "global_search" |
| 92 | |
| 93 | with pytest.raises(object_ai.InvalidChatRequestError, match="Unknown tools"): |
| 94 | object_ai.mcp_tools_as_provider_tools(["launch_missiles"], object_mcp.TOOLS, service="anthropic") |
| 95 | |
| 96 | |
| 97 | def test_run_chat_loops_through_tool_calls(): |
| 98 | responses = [ |
| 99 | anthropic_tool_response("global_search", {"query": "flywheel"}), |
| 100 | anthropic_text_response("Found one note about the flywheel."), |
| 101 | ] |
| 102 | requests_seen = [] |
| 103 | |
| 104 | def send_http(url, headers, body): |
| 105 | requests_seen.append(json.loads(body)) |
| 106 | return responses.pop(0) |
| 107 | |
| 108 | dispatched = [] |
| 109 | |
| 110 | def dispatch_tool(name, arguments): |
| 111 | dispatched.append((name, arguments)) |
| 112 | return {"http_status": 200, "response": {"results": {"notes": [{"id": "n1"}]}}} |
| 113 | |
| 114 | result = object_ai.run_chat( |
| 115 | send_http=send_http, |
| 116 | dispatch_tool=dispatch_tool, |
| 117 | service="anthropic", |
| 118 | model="claude-haiku-4-5", |
| 119 | key="sk-test", |
| 120 | message="find flywheel notes", |
| 121 | tools=object_ai.mcp_tools_as_provider_tools( |
| 122 | ["global_search"], object_mcp.TOOLS, service="anthropic" |
| 123 | ), |
| 124 | ) |
| 125 | |
| 126 | assert result["reply"] == "Found one note about the flywheel." |
| 127 | assert result["rounds"] == 2 |
| 128 | assert dispatched == [("global_search", {"query": "flywheel"})] |
| 129 | assert result["tool_calls"] == [ |
| 130 | {"name": "global_search", "arguments": {"query": "flywheel"}, "http_status": 200} |
| 131 | ] |
| 132 | assert result["usage"] == {"input_tokens": 30, "output_tokens": 13} |
| 133 | # The second provider round carries the tool result back. |
| 134 | followup = requests_seen[1]["messages"] |
| 135 | assert followup[-1]["content"][0]["type"] == "tool_result" |
| 136 | |
| 137 | |
| 138 | def test_read_page_tool_calls_echo_real_links_other_tools_do_not(): |
| 139 | """The bug this fixes: only the model's own restated prose survives |
| 140 | into a later turn's history, never a tool's actual result -- so a |
| 141 | read_page call's real hrefs were lost the moment the model finished |
| 142 | replying, and a later "go to that story" had nothing but the model's |
| 143 | memory of its own summary to resolve against. tool_calls now echoes |
| 144 | read_page's real (capped) link list back to the caller; every other |
| 145 | tool's entry is unchanged, since most results are either already |
| 146 | small or not the kind of thing a later turn needs to resolve a vague |
| 147 | reference against. |
| 148 | """ |
| 149 | responses = [ |
| 150 | anthropic_tool_response("read_page", {"url": "https://techmeme.com"}), |
| 151 | anthropic_text_response("TechMeme is showing top tech news."), |
| 152 | ] |
| 153 | |
| 154 | def send_http(url, headers, body): |
| 155 | return responses.pop(0) |
| 156 | |
| 157 | def dispatch_tool(name, arguments): |
| 158 | return { |
| 159 | "http_status": 200, |
| 160 | "response": { |
| 161 | "title": "Techmeme", |
| 162 | "text": "...", |
| 163 | "final_url": "https://techmeme.com", |
| 164 | "links": [{"n": i, "label": f"Story {i}", "href": f"https://example.com/{i}"} |
| 165 | for i in range(1, 50)], |
| 166 | }, |
| 167 | } |
| 168 | |
| 169 | result = object_ai.run_chat( |
| 170 | send_http=send_http, dispatch_tool=dispatch_tool, |
| 171 | service="anthropic", model="claude-haiku-4-5", key="sk-test", |
| 172 | message="read techmeme", |
| 173 | tools=object_ai.mcp_tools_as_provider_tools(["read_page"], object_mcp.TOOLS, service="anthropic"), |
| 174 | ) |
| 175 | |
| 176 | [call] = result["tool_calls"] |
| 177 | assert call["name"] == "read_page" |
| 178 | assert call["result"]["final_url"] == "https://techmeme.com" |
| 179 | assert call["result"]["title"] == "Techmeme" |
| 180 | # Capped, not the full 49 -- a link-heavy page must not balloon every |
| 181 | # later turn's context once a caller starts re-injecting this. |
| 182 | assert len(call["result"]["links"]) == object_ai.READER_LINKS_ECHO_CAP |
| 183 | assert call["result"]["links"][0] == {"n": 1, "label": "Story 1", "href": "https://example.com/1"} |
| 184 | |
| 185 | |
| 186 | def test_non_read_page_tool_calls_never_get_a_result_echo(): |
| 187 | responses = [ |
| 188 | anthropic_tool_response("global_search", {"query": "flywheel"}), |
| 189 | anthropic_text_response("Found one note."), |
| 190 | ] |
| 191 | |
| 192 | def send_http(url, headers, body): |
| 193 | return responses.pop(0) |
| 194 | |
| 195 | def dispatch_tool(name, arguments): |
| 196 | return {"http_status": 200, "response": {"results": {"notes": [{"id": "n1"}]}}} |
| 197 | |
| 198 | result = object_ai.run_chat( |
| 199 | send_http=send_http, dispatch_tool=dispatch_tool, |
| 200 | service="anthropic", model="claude-haiku-4-5", key="sk-test", |
| 201 | message="find flywheel notes", |
| 202 | tools=object_ai.mcp_tools_as_provider_tools(["global_search"], object_mcp.TOOLS, service="anthropic"), |
| 203 | ) |
| 204 | assert result["tool_calls"] == [ |
| 205 | {"name": "global_search", "arguments": {"query": "flywheel"}, "http_status": 200} |
| 206 | ] |
| 207 | |
| 208 | |
| 209 | def test_tool_result_summary_ignores_read_page_calls_with_no_links(): |
| 210 | assert object_ai._tool_result_summary("read_page", {"title": "x", "links": []}) is None |
| 211 | assert object_ai._tool_result_summary("read_page", {"title": "x"}) is None |
| 212 | assert object_ai._tool_result_summary("read_page", "not a dict") is None |
| 213 | assert object_ai._tool_result_summary("global_search", {"links": [{"n": 1}]}) is None |
| 214 | |
| 215 | |
| 216 | def test_run_chat_resumes_from_history(): |
| 217 | requests_seen = [] |
| 218 | |
| 219 | def send_http(url, headers, body): |
| 220 | requests_seen.append(json.loads(body)) |
| 221 | return anthropic_text_response("Continuing where we left off.") |
| 222 | |
| 223 | result = object_ai.run_chat( |
| 224 | send_http=send_http, |
| 225 | dispatch_tool=lambda name, arguments: {}, |
| 226 | service="anthropic", |
| 227 | model="claude-haiku-4-5", |
| 228 | key="sk-test", |
| 229 | message="and then?", |
| 230 | history=[ |
| 231 | {"role": "user", "content": "tell me about the flywheel"}, |
| 232 | {"role": "assistant", "content": "It spins."}, |
| 233 | ], |
| 234 | ) |
| 235 | assert result["reply"] == "Continuing where we left off." |
| 236 | messages = requests_seen[0]["messages"] |
| 237 | assert [m["role"] for m in messages] == ["user", "assistant", "user"] |
| 238 | assert messages[-1]["content"] == "and then?" |
| 239 | |
| 240 | with pytest.raises(object_ai.InvalidChatRequestError, match="history"): |
| 241 | object_ai.normalize_history([{"role": "system", "content": "x"}]) |
| 242 | |
| 243 | |
| 244 | def test_run_chat_stops_at_round_limit(): |
| 245 | def send_http(url, headers, body): |
| 246 | return anthropic_tool_response("global_search", {"query": "again"}) |
| 247 | |
| 248 | result = object_ai.run_chat( |
| 249 | send_http=send_http, |
| 250 | dispatch_tool=lambda name, arguments: {"http_status": 200, "response": {}}, |
| 251 | service="anthropic", |
| 252 | model="claude-haiku-4-5", |
| 253 | key="sk-test", |
| 254 | message="loop forever", |
| 255 | tools=object_ai.mcp_tools_as_provider_tools( |
| 256 | ["global_search"], object_mcp.TOOLS, service="anthropic" |
| 257 | ), |
| 258 | max_rounds=2, |
| 259 | ) |
| 260 | assert result["truncated"] is True |
| 261 | assert len(result["tool_calls"]) == 2 |
| 262 | |
| 263 | |
| 264 | def test_run_chat_raises_on_provider_error(): |
| 265 | def send_http(url, headers, body): |
| 266 | return 401, json.dumps({"error": {"message": "invalid x-api-key"}}).encode() |
| 267 | |
| 268 | with pytest.raises(object_ai.AIProviderError, match="invalid x-api-key"): |
| 269 | object_ai.run_chat( |
| 270 | send_http=send_http, |
| 271 | dispatch_tool=lambda name, arguments: {}, |
| 272 | service="anthropic", |
| 273 | model="claude-haiku-4-5", |
| 274 | key="sk-bad", |
| 275 | message="hi", |
| 276 | ) |
| 277 | |
| 278 | |
| 279 | def test_ai_chat_endpoint_runs_tools_with_caller_permissions(tmp_path, monkeypatch): |
| 280 | data_dir = tmp_path / "data" |
| 281 | write_records(data_dir, "notes", "id\tcontent\nn1\tflywheel plan\n") |
| 282 | write_records( |
| 283 | data_dir, |
| 284 | "ai_usage", |
| 285 | "id\towner_id\tprovider\tmodel\ttokens_in\ttokens_out\tcost_cents\tcreated_at\n", |
| 286 | ) |
| 287 | schema_file = data_dir / "schemas" / "notes.json" |
| 288 | schema_file.parent.mkdir(parents=True, exist_ok=True) |
| 289 | schema_file.write_text( |
| 290 | json.dumps( |
| 291 | {"fields": [{"name": "id"}, {"name": "content"}], "search": {"fields": ["content"]}} |
| 292 | ) |
| 293 | ) |
| 294 | save_permission_policy( |
| 295 | data_dir, |
| 296 | { |
| 297 | "access_mode": "role_based", |
| 298 | "rules": [ |
| 299 | { |
| 300 | "effect": "allow", |
| 301 | "principal": "registered", |
| 302 | "actions": ["read"], |
| 303 | "collection": "notes", |
| 304 | "reason": "signed-in users read notes", |
| 305 | } |
| 306 | ], |
| 307 | }, |
| 308 | ) |
| 309 | monkeypatch.setenv(object_server.DATA_DIR_ENV, str(data_dir)) |
| 310 | monkeypatch.setenv(object_server.AI_CHAT_ENABLED_ENV, "true") |
| 311 | monkeypatch.setenv(object_server.PERMISSION_ENFORCEMENT_ENV, "true") |
| 312 | enable_admin_token(monkeypatch) |
| 313 | token, _ = create_identity_session({"user_id": "dan"}) |
| 314 | bearer = [("authorization", f"Bearer {token}")] |
| 315 | |
| 316 | responses = [ |
| 317 | anthropic_tool_response("global_search", {"query": "flywheel"}), |
| 318 | anthropic_text_response("One note matches."), |
| 319 | ] |
| 320 | monkeypatch.setattr( |
| 321 | object_server.object_ai, |
| 322 | "run_chat", |
| 323 | object_ai.run_chat, |
| 324 | ) |
| 325 | |
| 326 | def fake_urlopen(request_obj, timeout=None): |
| 327 | raise AssertionError("network must go through the injected transport") |
| 328 | |
| 329 | # Intercept at the handler's transport: patch urllib inside object_server. |
| 330 | class FakeResponse: |
| 331 | def __init__(self, status, body): |
| 332 | self.status = status |
| 333 | self._body = body |
| 334 | |
| 335 | def read(self): |
| 336 | return self._body |
| 337 | |
| 338 | def __enter__(self): |
| 339 | return self |
| 340 | |
| 341 | def __exit__(self, *args): |
| 342 | return False |
| 343 | |
| 344 | def fake_transport(request_obj, timeout=None): |
| 345 | status, body = responses.pop(0) |
| 346 | return FakeResponse(status, body) |
| 347 | |
| 348 | monkeypatch.setattr(object_server.urllib.request, "urlopen", fake_transport) |
| 349 | |
| 350 | # No key stored yet: helpful 400. |
| 351 | status, _, no_key = request( |
| 352 | "/api/ai/chat", |
| 353 | method="POST", |
| 354 | body=json.dumps({"message": "find flywheel notes", "tools": ["global_search"]}).encode(), |
| 355 | headers=bearer + [("content-type", "application/json")], |
| 356 | ) |
| 357 | assert status == 400 and "service-keys" in no_key["error"] |
| 358 | |
| 359 | request( |
| 360 | "/identity/users/dan/service-keys", |
| 361 | method="PUT", |
| 362 | body=json.dumps({"service": "anthropic", "key": "sk-test-1"}).encode(), |
| 363 | headers=bearer + [("content-type", "application/json")], |
| 364 | ) |
| 365 | |
| 366 | status, _, chat = request( |
| 367 | "/api/ai/chat", |
| 368 | method="POST", |
| 369 | body=json.dumps({"message": "find flywheel notes", "tools": ["global_search"]}).encode(), |
| 370 | headers=bearer + [("content-type", "application/json")], |
| 371 | ) |
| 372 | |
| 373 | assert status == 200, chat |
| 374 | assert chat["reply"] == "One note matches." |
| 375 | assert chat["tool_calls"][0]["name"] == "global_search" |
| 376 | assert chat["tool_calls"][0]["http_status"] == 200 |
| 377 | assert chat["usage"]["output_tokens"] == 13 |
| 378 | |
| 379 | |
| 380 | def test_select_price_row_picks_most_recent_effective_date(): |
| 381 | rows = [ |
| 382 | { |
| 383 | "provider": "anthropic", |
| 384 | "model": "claude-haiku-4-5", |
| 385 | "input_per_million_cents": "80", |
| 386 | "output_per_million_cents": "400", |
| 387 | "effective_date": "2025-01-01", |
| 388 | }, |
| 389 | { |
| 390 | "provider": "anthropic", |
| 391 | "model": "claude-haiku-4-5", |
| 392 | "input_per_million_cents": "100", |
| 393 | "output_per_million_cents": "500", |
| 394 | "effective_date": "2026-01-01", |
| 395 | }, |
| 396 | { |
| 397 | "provider": "openai", |
| 398 | "model": "gpt-5-mini", |
| 399 | "input_per_million_cents": "25", |
| 400 | "output_per_million_cents": "200", |
| 401 | "effective_date": "2026-01-01", |
| 402 | }, |
| 403 | ] |
| 404 | row = object_ai.select_price_row(rows, provider="anthropic", model="claude-haiku-4-5") |
| 405 | assert row["effective_date"] == "2026-01-01" |
| 406 | assert row["input_per_million_cents"] == "100" |
| 407 | |
| 408 | assert object_ai.select_price_row(rows, provider="anthropic", model="claude-opus-4-8") is None |
| 409 | assert object_ai.select_price_row([], provider="anthropic", model="claude-haiku-4-5") is None |
| 410 | |
| 411 | |
| 412 | def test_compute_cost_cents_exact_integer_math(): |
| 413 | price_row = {"input_per_million_cents": "100", "output_per_million_cents": "500"} |
| 414 | # 2.5M input tokens @ 100c/M = 250c; 1.5M output tokens @ 500c/M = 750c. |
| 415 | assert object_ai.compute_cost_cents(2_500_000, 1_500_000, price_row) == 1000 |
| 416 | # Floors rather than rounds: 999,999 tokens @ 100c/M is 99.9999c, not 100c. |
| 417 | assert object_ai.compute_cost_cents(999_999, 0, price_row) == 99 |
| 418 | assert object_ai.compute_cost_cents(0, 0, price_row) == 0 |
| 419 | # No price row -> cost is None, never zero (zero would look like a real free turn). |
| 420 | assert object_ai.compute_cost_cents(1000, 1000, None) is None |
| 421 | |
| 422 | |
| 423 | def test_compute_cost_cents_sums_before_flooring(): |
| 424 | # A cheap-model price row (gpt-5-mini's real rate): flooring each side |
| 425 | # separately before summing would give 0 + 0 = 0c for this call even |
| 426 | # though the true combined cost is ~1.06c -- real money silently |
| 427 | # recorded as free. Summing first and flooring once catches it. |
| 428 | price_row = {"input_per_million_cents": 25, "output_per_million_cents": 200} |
| 429 | assert object_ai.compute_cost_cents(36_428, 734, price_row) == 1 |
| 430 | # A call whose combined cost genuinely is sub-cent still floors to 0 -- |
| 431 | # that's correct integer-cent accounting, not the bug. |
| 432 | assert object_ai.compute_cost_cents(644, 1024, price_row) == 0 |
| 433 | |
| 434 | |
| 435 | def test_openai_request_sets_reasoning_effort_none_for_gpt_5_6_family_with_tools(): |
| 436 | # OpenAI's own error, hit live: "Function tools with reasoning_effort |
| 437 | # are not supported for gpt-5.6-luna in /v1/chat/completions ... set |
| 438 | # reasoning_effort to 'none'." Scoped to the 5.6 family specifically -- |
| 439 | # gpt-5/gpt-5-mini already work with tools under their own default and |
| 440 | # must NOT get this override. |
| 441 | captured_bodies = [] |
| 442 | |
| 443 | def send_http(url, headers, body): |
| 444 | captured_bodies.append(json.loads(body)) |
| 445 | return openai_text_response_with_usage("ok", prompt_tokens=5, completion_tokens=2) |
| 446 | |
| 447 | tools = [{"type": "function", "function": {"name": "noop", "parameters": {}}}] |
| 448 | |
| 449 | object_ai.run_chat( |
| 450 | send_http=send_http, dispatch_tool=lambda name, arguments: {}, |
| 451 | service="openai", model="gpt-5.6-luna", key="sk-test", |
| 452 | message="hello", tools=tools, |
| 453 | ) |
| 454 | assert captured_bodies[-1]["reasoning_effort"] == "none" |
| 455 | |
| 456 | captured_bodies.clear() |
| 457 | object_ai.run_chat( |
| 458 | send_http=send_http, dispatch_tool=lambda name, arguments: {}, |
| 459 | service="openai", model="gpt-5-mini", key="sk-test", |
| 460 | message="hello", tools=tools, |
| 461 | ) |
| 462 | assert "reasoning_effort" not in captured_bodies[-1] |
| 463 | |
| 464 | captured_bodies.clear() |
| 465 | object_ai.run_chat( |
| 466 | send_http=send_http, dispatch_tool=lambda name, arguments: {}, |
| 467 | service="openai", model="gpt-5.6-luna", key="sk-test", |
| 468 | message="hello", tools=None, |
| 469 | ) |
| 470 | assert "reasoning_effort" not in captured_bodies[-1] |
| 471 | |
| 472 | |
| 473 | def test_run_chat_openai_usage_shape_feeds_cost_math(): |
| 474 | def send_http(url, headers, body): |
| 475 | return openai_text_response_with_usage( |
| 476 | "hi", prompt_tokens=1_000_000, completion_tokens=500_000 |
| 477 | ) |
| 478 | |
| 479 | result = object_ai.run_chat( |
| 480 | send_http=send_http, |
| 481 | dispatch_tool=lambda name, arguments: {}, |
| 482 | service="openai", |
| 483 | model="gpt-5-mini", |
| 484 | key="sk-test", |
| 485 | message="hello", |
| 486 | ) |
| 487 | assert result["usage"] == {"input_tokens": 1_000_000, "output_tokens": 500_000} |
| 488 | |
| 489 | price_row = {"input_per_million_cents": 25, "output_per_million_cents": 200} |
| 490 | # 1M input @ 25c/M = 25c; 0.5M output @ 200c/M = 100c. |
| 491 | assert ( |
| 492 | object_ai.compute_cost_cents( |
| 493 | result["usage"]["input_tokens"], result["usage"]["output_tokens"], price_row |
| 494 | ) |
| 495 | == 125 |
| 496 | ) |
| 497 | |
| 498 | |
| 499 | def test_ai_chat_endpoint_records_cost_and_writes_usage(tmp_path, monkeypatch): |
| 500 | data_dir = tmp_path / "data" |
| 501 | write_records( |
| 502 | data_dir, |
| 503 | "ai_prices", |
| 504 | "id\tprovider\tmodel\tinput_per_million_cents\toutput_per_million_cents\teffective_date\n" |
| 505 | "p-old\tanthropic\tclaude-haiku-4-5\t80\t400\t2025-01-01\n" |
| 506 | "p-new\tanthropic\tclaude-haiku-4-5\t100\t500\t2026-01-01\n", |
| 507 | ) |
| 508 | schema_file = data_dir / "schemas" / "ai_usage.json" |
| 509 | schema_file.parent.mkdir(parents=True, exist_ok=True) |
| 510 | schema_file.write_text( |
| 511 | json.dumps( |
| 512 | { |
| 513 | "fields": [ |
| 514 | {"name": "id"}, |
| 515 | {"name": "owner_id", "type": "text"}, |
| 516 | {"name": "provider", "type": "text", "required": True}, |
| 517 | {"name": "model", "type": "text", "required": True}, |
| 518 | {"name": "tokens_in", "type": "int", "required": True}, |
| 519 | {"name": "tokens_out", "type": "int", "required": True}, |
| 520 | {"name": "cost_cents", "type": "int"}, |
| 521 | {"name": "created_at", "type": "datetime", "read_only": True}, |
| 522 | ] |
| 523 | } |
| 524 | ) |
| 525 | ) |
| 526 | |
| 527 | monkeypatch.setenv(object_server.DATA_DIR_ENV, str(data_dir)) |
| 528 | monkeypatch.setenv(object_server.AI_CHAT_ENABLED_ENV, "true") |
| 529 | enable_admin_token(monkeypatch) |
| 530 | token, _ = create_identity_session({"user_id": "dan"}) |
| 531 | bearer = [("authorization", f"Bearer {token}")] |
| 532 | |
| 533 | request( |
| 534 | "/identity/users/dan/service-keys", |
| 535 | method="PUT", |
| 536 | body=json.dumps({"service": "anthropic", "key": "sk-test-1"}).encode(), |
| 537 | headers=bearer + [("content-type", "application/json")], |
| 538 | ) |
| 539 | |
| 540 | response = anthropic_text_response_with_usage( |
| 541 | "hi there", input_tokens=2_500_000, output_tokens=1_500_000 |
| 542 | ) |
| 543 | |
| 544 | def fake_transport(request_obj, timeout=None): |
| 545 | status, body = response |
| 546 | |
| 547 | class FakeResponse: |
| 548 | def __init__(self): |
| 549 | self.status = status |
| 550 | |
| 551 | def read(self): |
| 552 | return body |
| 553 | |
| 554 | def __enter__(self): |
| 555 | return self |
| 556 | |
| 557 | def __exit__(self, *args): |
| 558 | return False |
| 559 | |
| 560 | return FakeResponse() |
| 561 | |
| 562 | monkeypatch.setattr(object_server.urllib.request, "urlopen", fake_transport) |
| 563 | |
| 564 | status, _, chat = request( |
| 565 | "/api/ai/chat", |
| 566 | method="POST", |
| 567 | body=json.dumps( |
| 568 | {"message": "hi", "model": "anthropic:claude-haiku-4-5", "tools": []} |
| 569 | ).encode(), |
| 570 | headers=bearer + [("content-type", "application/json")], |
| 571 | ) |
| 572 | |
| 573 | assert status == 200, chat |
| 574 | assert chat["usage"]["input_tokens"] == 2_500_000 |
| 575 | assert chat["usage"]["output_tokens"] == 1_500_000 |
| 576 | # Must pick the 2026-01-01 row (100/500), not the older 2025-01-01 row (80/400): |
| 577 | # 2.5M * 100c/M + 1.5M * 500c/M = 250c + 750c = 1000c. |
| 578 | assert chat["usage"]["cost_cents"] == 1000 |
| 579 | |
| 580 | usage_rows = object_records.read_collection_records("ai_usage", base_dir=data_dir) |
| 581 | assert len(usage_rows) == 1 |
| 582 | row = usage_rows[0] |
| 583 | assert row["owner_id"] == "dan" |
| 584 | assert row["provider"] == "anthropic" |
| 585 | assert row["model"] == "claude-haiku-4-5" |
| 586 | assert row["tokens_in"] == "2500000" |
| 587 | assert row["tokens_out"] == "1500000" |
| 588 | assert row["cost_cents"] == "1000" |
| 589 | assert row["created_at"] # server-stamped, never blank |
| 590 | |
| 591 | # Server-side write, attributed to the calling user -- not the client. |
| 592 | changes = object_record_changes.list_record_changes("ai_usage", base_dir=data_dir) |
| 593 | assert changes["changes"][0]["actor"] == "dan" |
| 594 | assert changes["changes"][0]["action"] == "create" |
| 595 | |
| 596 | |
| 597 | def test_ai_chat_endpoint_records_tokens_with_null_cost_when_price_missing(tmp_path, monkeypatch): |
| 598 | data_dir = tmp_path / "data" |
| 599 | # ai_prices has no row at all -- the collection is known but empty. |
| 600 | write_records( |
| 601 | data_dir, |
| 602 | "ai_prices", |
| 603 | "id\tprovider\tmodel\tinput_per_million_cents\toutput_per_million_cents\teffective_date\n", |
| 604 | ) |
| 605 | schema_file = data_dir / "schemas" / "ai_usage.json" |
| 606 | schema_file.parent.mkdir(parents=True, exist_ok=True) |
| 607 | schema_file.write_text( |
| 608 | json.dumps( |
| 609 | { |
| 610 | "fields": [ |
| 611 | {"name": "id"}, |
| 612 | {"name": "owner_id", "type": "text"}, |
| 613 | {"name": "provider", "type": "text", "required": True}, |
| 614 | {"name": "model", "type": "text", "required": True}, |
| 615 | {"name": "tokens_in", "type": "int", "required": True}, |
| 616 | {"name": "tokens_out", "type": "int", "required": True}, |
| 617 | {"name": "cost_cents", "type": "int"}, |
| 618 | {"name": "created_at", "type": "datetime", "read_only": True}, |
| 619 | ] |
| 620 | } |
| 621 | ) |
| 622 | ) |
| 623 | |
| 624 | monkeypatch.setenv(object_server.DATA_DIR_ENV, str(data_dir)) |
| 625 | monkeypatch.setenv(object_server.AI_CHAT_ENABLED_ENV, "true") |
| 626 | enable_admin_token(monkeypatch) |
| 627 | token, _ = create_identity_session({"user_id": "dan"}) |
| 628 | bearer = [("authorization", f"Bearer {token}")] |
| 629 | |
| 630 | request( |
| 631 | "/identity/users/dan/service-keys", |
| 632 | method="PUT", |
| 633 | body=json.dumps({"service": "anthropic", "key": "sk-test-1"}).encode(), |
| 634 | headers=bearer + [("content-type", "application/json")], |
| 635 | ) |
| 636 | |
| 637 | response = anthropic_text_response_with_usage( |
| 638 | "hi there", input_tokens=42, output_tokens=7 |
| 639 | ) |
| 640 | |
| 641 | def fake_transport(request_obj, timeout=None): |
| 642 | status, body = response |
| 643 | |
| 644 | class FakeResponse: |
| 645 | def __init__(self): |
| 646 | self.status = status |
| 647 | |
| 648 | def read(self): |
| 649 | return body |
| 650 | |
| 651 | def __enter__(self): |
| 652 | return self |
| 653 | |
| 654 | def __exit__(self, *args): |
| 655 | return False |
| 656 | |
| 657 | return FakeResponse() |
| 658 | |
| 659 | monkeypatch.setattr(object_server.urllib.request, "urlopen", fake_transport) |
| 660 | |
| 661 | status, _, chat = request( |
| 662 | "/api/ai/chat", |
| 663 | method="POST", |
| 664 | body=json.dumps( |
| 665 | {"message": "hi", "model": "anthropic:claude-mystery-model", "tools": []} |
| 666 | ).encode(), |
| 667 | headers=bearer + [("content-type", "application/json")], |
| 668 | ) |
| 669 | |
| 670 | # Missing price row never fails the chat -- the reply still comes back. |
| 671 | assert status == 200, chat |
| 672 | assert chat["reply"] == "hi there" |
| 673 | assert chat["usage"]["cost_cents"] is None |
| 674 | |
| 675 | usage_rows = object_records.read_collection_records("ai_usage", base_dir=data_dir) |
| 676 | assert len(usage_rows) == 1 |
| 677 | row = usage_rows[0] |
| 678 | # Tokens are still recorded even though there is nothing to price them with. |
| 679 | assert row["tokens_in"] == "42" |
| 680 | assert row["tokens_out"] == "7" |
| 681 | assert row.get("cost_cents", "") == "" |
| 682 | |
| 683 | |
| 684 | def test_ai_chat_endpoint_requires_flag_and_session(tmp_path, monkeypatch): |
| 685 | monkeypatch.setenv(object_server.DATA_DIR_ENV, str(tmp_path)) |
| 686 | enable_admin_token(monkeypatch) |
| 687 | |
| 688 | status, _, disabled = request( |
| 689 | "/api/ai/chat", method="POST", body=json.dumps({"message": "hi"}).encode() |
| 690 | ) |
| 691 | assert status == 403 and "disabled" in disabled["error"] |
| 692 | |
| 693 | monkeypatch.setenv(object_server.AI_CHAT_ENABLED_ENV, "true") |
| 694 | status, _, anonymous = request( |
| 695 | "/api/ai/chat", method="POST", body=json.dumps({"message": "hi"}).encode() |
| 696 | ) |
| 697 | assert status == 401 |
| 698 | |
| 699 | |
| 700 | def test_ai_chat_endpoint_records_the_actual_reply_text(tmp_path, monkeypatch): |
| 701 | """The bug a session review caught, live: every server-recorded turn |
| 702 | had output == "" regardless of what the model said, because the |
| 703 | write read result.get("text") and run_chat's key is "reply". The |
| 704 | HTTP response spreads **result directly, so the client always saw |
| 705 | the correct reply -- this was invisible everywhere except the |
| 706 | history the server-side write exists specifically to make |
| 707 | inspectable, and it went unnoticed for as long as it did because |
| 708 | every source-presence test checked that "shell_commands" and |
| 709 | "created_at" appeared in the file, never the recorded VALUE. This |
| 710 | reads the row back and checks the text, not the source. |
| 711 | """ |
| 712 | data_dir = tmp_path / "data" |
| 713 | schema_file = data_dir / "schemas" / "shell_commands.json" |
| 714 | schema_file.parent.mkdir(parents=True, exist_ok=True) |
| 715 | schema_file.write_text(json.dumps({"fields": [ |
| 716 | {"name": "id"}, {"name": "input", "type": "text"}, |
| 717 | {"name": "output", "type": "text"}, {"name": "kind", "type": "text"}, |
| 718 | {"name": "owner_id", "type": "text"}, {"name": "session_id", "type": "text"}, |
| 719 | {"name": "source", "type": "text"}, {"name": "model", "type": "text"}, |
| 720 | {"name": "duration_ms", "type": "int"}, {"name": "tool_calls", "type": "text"}, |
| 721 | {"name": "created_at", "type": "datetime"}, # NOT read_only, matching the real schema |
| 722 | ]})) |
| 723 | (data_dir / "schemas" / "ai_usage.json").write_text(json.dumps({"fields": [ |
| 724 | {"name": "id"}, {"name": "owner_id", "type": "text"}, |
| 725 | {"name": "provider", "type": "text", "required": True}, |
| 726 | {"name": "model", "type": "text", "required": True}, |
| 727 | {"name": "tokens_in", "type": "int", "required": True}, |
| 728 | {"name": "tokens_out", "type": "int", "required": True}, |
| 729 | {"name": "cost_cents", "type": "int"}, |
| 730 | {"name": "created_at", "type": "datetime", "read_only": True}, |
| 731 | ]})) |
| 732 | |
| 733 | monkeypatch.setenv(object_server.DATA_DIR_ENV, str(data_dir)) |
| 734 | monkeypatch.setenv(object_server.AI_CHAT_ENABLED_ENV, "true") |
| 735 | enable_admin_token(monkeypatch) |
| 736 | token, _ = create_identity_session({"user_id": "dan"}) |
| 737 | bearer = [("authorization", f"Bearer {token}")] |
| 738 | |
| 739 | request( |
| 740 | "/identity/users/dan/service-keys", method="PUT", |
| 741 | body=json.dumps({"service": "anthropic", "key": "sk-test-1"}).encode(), |
| 742 | headers=bearer + [("content-type", "application/json")], |
| 743 | ) |
| 744 | |
| 745 | status, body = anthropic_text_response("Here are your 8 notes on screen, sorted newest first.") |
| 746 | |
| 747 | def fake_transport(request_obj, timeout=None): |
| 748 | class FakeResponse: |
| 749 | def __init__(self): |
| 750 | self.status = status |
| 751 | def read(self): |
| 752 | return body |
| 753 | def __enter__(self): |
| 754 | return self |
| 755 | def __exit__(self, *args): |
| 756 | return False |
| 757 | return FakeResponse() |
| 758 | |
| 759 | monkeypatch.setattr(object_server.urllib.request, "urlopen", fake_transport) |
| 760 | |
| 761 | resp_status, _, chat = request( |
| 762 | "/api/ai/chat", method="POST", |
| 763 | body=json.dumps({"message": "show me my notes", |
| 764 | "model": "anthropic:claude-haiku-4-5", "tools": [], |
| 765 | "session_id": "s-test-1", "source": "talk"}).encode(), |
| 766 | headers=bearer + [("content-type", "application/json")], |
| 767 | ) |
| 768 | assert resp_status == 200, chat |
| 769 | assert chat["reply"] == "Here are your 8 notes on screen, sorted newest first." |
| 770 | |
| 771 | rows = object_records.read_collection_records("shell_commands", base_dir=data_dir) |
| 772 | assert len(rows) == 1, rows |
| 773 | row = rows[0] |
| 774 | assert row["input"] == "show me my notes" |
| 775 | # THE assertion the bug would have failed. |
| 776 | assert row["output"] == "Here are your 8 notes on screen, sorted newest first." |
| 777 | assert row["session_id"] == "s-test-1" |
| 778 | assert row["source"] == "talk" |
| 779 | assert row["created_at"] |