feat: harden cache reuse semantics and expand protocol regressions

Stabilize cross-protocol ask-mode/streaming behavior and reduce session-reuse branch collisions, then add focused docs/tests for multimodal normalization and pool/stats/config paths.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
GitHub Actions
2026-04-20 14:26:11 +08:00
parent b96b91e5b7
commit 12a4d9584e
9 changed files with 441 additions and 55 deletions

View File

@@ -2,7 +2,7 @@ from __future__ import annotations
import unittest
from app.session_cache import SessionCache, hash_user_context
from app.session_cache import SessionCache, hash_branch_context, hash_user_context
class SessionCacheToolingTests(unittest.IsolatedAsyncioTestCase):
@@ -17,6 +17,21 @@ class SessionCacheToolingTests(unittest.IsolatedAsyncioTestCase):
]
self.assertEqual(hash_user_context(base), hash_user_context(with_extra))
def test_hash_branch_context_distinguishes_assistant_tool_branch(self) -> None:
base = [
{"role": "system", "content": "S"},
{"role": "user", "content": "U"},
{"role": "assistant", "content": "A1"},
{"role": "tool", "content": "T1", "tool_call_id": "call-1"},
]
changed = [
{"role": "system", "content": "S"},
{"role": "user", "content": "U"},
{"role": "assistant", "content": "A2"},
{"role": "tool", "content": "T1", "tool_call_id": "call-1"},
]
self.assertNotEqual(hash_branch_context(base), hash_branch_context(changed))
def test_build_key_changes_with_tool_config(self) -> None:
cache = SessionCache(max_entries=8, ttl_sec=60)
msgs = [{"role": "user", "content": "hi"}]
@@ -26,6 +41,14 @@ class SessionCacheToolingTests(unittest.IsolatedAsyncioTestCase):
self.assertEqual(key1, key2)
self.assertNotEqual(key1, key3)
def test_build_key_keeps_legacy_shape_without_branch_context(self) -> None:
cache = SessionCache(max_entries=8, ttl_sec=60)
msgs = [{"role": "user", "content": "hi"}]
legacy = cache.build_key("k", msgs)
with_branch = cache.build_key("k", msgs, branch_context="abc")
self.assertEqual(legacy.count(":"), 2)
self.assertEqual(with_branch.count(":"), 3)
async def test_lru_evicts_oldest(self) -> None:
cache = SessionCache(max_entries=2, ttl_sec=600)
await cache.put("k1", "s1")