perf(agent): append runtime context after user content for cache stability

Runtime context (time, channel, sender) changes every turn, so placing
it before user content invalidated the prompt-cache prefix. Appending it
after user content keeps the prefix stable and improves KV cache hit
rates. The stripping logic in _save_turn was simplified from 16 lines
to 6 as a side benefit.
This commit is contained in:
chengyongru
2026-05-15 23:06:37 +08:00
committed by Xubin Ren
parent 164614ccf2
commit 0f3677c0d8
4 changed files with 68 additions and 24 deletions
+36 -2
View File
@@ -101,8 +101,8 @@ def test_save_turn_keeps_image_placeholder_with_path_after_runtime_strip() -> No
[{
"role": "user",
"content": [
{"type": "text", "text": runtime},
{"type": "image_url", "image_url": {"url": "data:image/png;base64,abc"}, "_meta": {"path": "/media/feishu/photo.jpg"}},
{"type": "text", "text": runtime},
],
}],
skip=0,
@@ -120,8 +120,8 @@ def test_save_turn_keeps_image_placeholder_without_meta() -> None:
[{
"role": "user",
"content": [
{"type": "text", "text": runtime},
{"type": "image_url", "image_url": {"url": "data:image/png;base64,abc"}},
{"type": "text", "text": runtime},
],
}],
skip=0,
@@ -129,6 +129,40 @@ def test_save_turn_keeps_image_placeholder_without_meta() -> None:
assert session.messages[0]["content"] == [{"type": "text", "text": "[image]"}]
def test_save_turn_strips_runtime_context_suffix_from_string() -> None:
loop = _mk_loop()
session = Session(key="test:suffix-strip")
runtime = (
ContextBuilder._RUNTIME_CONTEXT_TAG
+ "\nCurrent Time: now\n"
+ ContextBuilder._RUNTIME_CONTEXT_END
)
loop._save_turn(
session,
[{"role": "user", "content": f"hello world\n\n{runtime}"}],
skip=0,
)
assert session.messages[0]["content"] == "hello world"
def test_save_turn_skips_string_user_when_only_runtime_context_suffix() -> None:
loop = _mk_loop()
session = Session(key="test:suffix-only")
runtime = (
ContextBuilder._RUNTIME_CONTEXT_TAG
+ "\nCurrent Time: now\n"
+ ContextBuilder._RUNTIME_CONTEXT_END
)
loop._save_turn(
session,
[{"role": "user", "content": runtime}],
skip=0,
)
assert session.messages == []
def test_save_turn_keeps_tool_results_under_16k() -> None:
loop = _mk_loop()
session = Session(key="test:tool-result")