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.
test_ai_prompt_caching.py · 4.1 KB · 102 lines
| 1 | """Anthropic requests mark the stable prefix for prompt caching. |
| 2 | |
| 3 | A request renders as tools -> system -> messages, and a cache entry is a |
| 4 | prefix of that. The breakpoints sit on the last tool and the stable system |
| 5 | text; per-request text (the local time, the last page read) is a separate |
| 6 | system block after them. The shell used to append the current time to its |
| 7 | system string, which made every request's prefix unique. |
| 8 | """ |
| 9 | |
| 10 | import json |
| 11 | import re |
| 12 | |
| 13 | import object_ai |
| 14 | import object_mcp |
| 15 | from ai_fakes import anthropic_message, chat_server, install_fake_transport, text_block |
| 16 | from test_app_talk_object import SHELL_SOURCE, TALK_SOURCE |
| 17 | from test_object_server import request |
| 18 | |
| 19 | |
| 20 | def _capture(service="anthropic", **kwargs): |
| 21 | bodies = [] |
| 22 | |
| 23 | def send_http(url, headers, body): |
| 24 | bodies.append(json.loads(body)) |
| 25 | if service == "anthropic": |
| 26 | return anthropic_message([text_block("ok")]) |
| 27 | return 200, json.dumps({"choices": [{"message": {"content": "ok"}}], |
| 28 | "usage": {"prompt_tokens": 1, "completion_tokens": 1}}).encode() |
| 29 | |
| 30 | object_ai.run_chat( |
| 31 | send_http=send_http, dispatch_tool=lambda n, a: {}, |
| 32 | service=service, model=kwargs.pop("model", "claude-haiku-5-5"), key="sk-test", |
| 33 | message="hello", **kwargs, |
| 34 | ) |
| 35 | return bodies[0] |
| 36 | |
| 37 | |
| 38 | def _tools(service="anthropic"): |
| 39 | return object_ai.mcp_tools_as_provider_tools( |
| 40 | ["global_search", "get_record"], object_mcp.TOOLS, service=service) |
| 41 | |
| 42 | |
| 43 | def test_last_tool_and_stable_system_carry_the_breakpoints(): |
| 44 | body = _capture(system="You are the shell.", context="Now: 12:00", tools=_tools()) |
| 45 | assert [("cache_control" in t) for t in body["tools"]] == [False, True] |
| 46 | assert body["tools"][-1]["cache_control"] == {"type": "ephemeral"} |
| 47 | assert body["system"] == [ |
| 48 | {"type": "text", "text": "You are the shell.", "cache_control": {"type": "ephemeral"}}, |
| 49 | {"type": "text", "text": "Now: 12:00"}, |
| 50 | ] |
| 51 | # Nothing in the messages carries a breakpoint. |
| 52 | assert "cache_control" not in json.dumps(body["messages"]) |
| 53 | |
| 54 | |
| 55 | def test_volatile_context_never_lands_before_the_cached_prefix(): |
| 56 | body = _capture(system="Stable.", context="Current local date/time: 12:34:56", tools=_tools()) |
| 57 | rendered_prefix = json.dumps(body["tools"]) + json.dumps(body["system"][0]) |
| 58 | assert "12:34:56" not in rendered_prefix |
| 59 | |
| 60 | |
| 61 | def test_caller_tool_definitions_are_not_mutated(): |
| 62 | tools = _tools() |
| 63 | _capture(system="s", tools=tools) |
| 64 | assert all("cache_control" not in t for t in tools) |
| 65 | |
| 66 | |
| 67 | def test_cache_off_sends_no_breakpoints(): |
| 68 | body = _capture(system="s", context="c", tools=_tools(), cache=False) |
| 69 | assert "cache_control" not in json.dumps(body) |
| 70 | |
| 71 | |
| 72 | def test_no_system_and_no_tools_sends_no_system_field(): |
| 73 | body = _capture() |
| 74 | assert "system" not in body and "tools" not in body |
| 75 | |
| 76 | |
| 77 | def test_openai_gets_system_and_context_as_one_system_message(): |
| 78 | body = _capture(service="openai", model="gpt-5-mini", system="Stable.", context="Now.", |
| 79 | tools=_tools("openai")) |
| 80 | assert body["messages"][0] == {"role": "system", "content": "Stable.\n\nNow."} |
| 81 | assert "cache_control" not in json.dumps(body) |
| 82 | |
| 83 | |
| 84 | def test_endpoint_passes_context_apart_from_system(tmp_path, monkeypatch): |
| 85 | _, headers = chat_server(tmp_path, monkeypatch) |
| 86 | seen = [] |
| 87 | install_fake_transport(monkeypatch, [anthropic_message([text_block("ok")])], seen) |
| 88 | status, _, chat = request( |
| 89 | "/api/ai/chat", method="POST", headers=headers, |
| 90 | body=json.dumps({"message": "hi", "system": "Stable.", "context": "Now: 9am"}).encode(), |
| 91 | ) |
| 92 | assert status == 200, chat |
| 93 | assert seen[0]["body"]["system"][0]["text"] == "Stable." |
| 94 | assert seen[0]["body"]["system"][1] == {"type": "text", "text": "Now: 9am"} |
| 95 | |
| 96 | |
| 97 | def test_shell_and_talk_send_the_time_as_context_not_inside_system(): |
| 98 | for source in (SHELL_SOURCE, TALK_SOURCE): |
| 99 | assert re.search(r'context: "Current local date/time: " \+ new Date\(\)', source) |
| 100 | assert not re.search(r'system:[^\n]*new Date\(\)', source) |
| 101 | assert '" Current local date/time: " + new Date()' not in source |
| 102 | assert "readerContextNote()}," in TALK_SOURCE |