Skip to content

Official Codebase of 3DCodeVerse

Python

cleanup: B1 — BoundedCache[T] in models/parts.py

gaoypeng2fd38160a8a2
diff --git a/harness/codeverse/models/anthropic_convert.py b/harness/codeverse/models/anthropic_convert.py
index 96080370..c393e5ca 100644
--- a/harness/codeverse/models/anthropic_convert.py
+++ b/harness/codeverse/models/anthropic_convert.py
@@ -12,8 +12,6 @@ Model-family rules (Anthropic API, 2026-08):
from __future__ import annotations
import json
-import threading
-from collections import OrderedDict
from typing import Any
from codeverse.contracts.chat import (
@@ -26,7 +24,7 @@ from codeverse.contracts.chat import (
ToolSpec,
)
from codeverse.models.base import ModelError
-from codeverse.models.parts import image_b64
+from codeverse.models.parts import BoundedCache, image_b64
from codeverse.models.schema_utils import to_anthropic_schema
SUBMIT_TOOL = "submit"
@@ -62,31 +60,10 @@ def sampling_allowed(model: str) -> bool:
return not any(tok in m for tok in _NO_SAMPLING)
-class _ThinkingCache:
- """Assistant turns that contain tool calls must be replayed WITH their
- thinking blocks on the next request. ``ToolCallPart`` cannot carry them,
- so we remember the blocks under the first tool-call id (bounded LRU)."""
-
- def __init__(self, capacity: int = 2048) -> None:
- self._d: OrderedDict[str, list[dict[str, Any]]] = OrderedDict()
- self._cap = capacity
- self._lock = threading.Lock()
-
- def put(self, call_id: str, blocks: list[dict[str, Any]]) -> None:
- if not blocks:
- return
- with self._lock:
- self._d[call_id] = blocks
- self._d.move_to_end(call_id)
- while len(self._d) > self._cap:
- self._d.popitem(last=False)
-
- def get(self, call_id: str) -> list[dict[str, Any]]:
- with self._lock:
- return list(self._d.get(call_id, []))
-
-
-THINKING_BLOCKS = _ThinkingCache()
+#: Assistant turns that contain tool calls must be replayed WITH their
+#: thinking blocks on the next request. ``ToolCallPart`` cannot carry them,
+#: so we remember the blocks under the first tool-call id (bounded LRU).
+THINKING_BLOCKS: BoundedCache[list[dict[str, Any]]] = BoundedCache(2048)
# ------------------------------------------------------------------ messages
@@ -135,7 +112,7 @@ def to_messages(messages: list[ChatMessage]) -> list[dict[str, Any]]:
if not blocks:
continue
if role == "assistant" and first_call_id:
- blocks = THINKING_BLOCKS.get(first_call_id) + blocks
+ blocks = list(THINKING_BLOCKS.get(first_call_id) or []) + blocks
if out and out[-1]["role"] == role:
out[-1]["content"].extend(blocks)
else:
diff --git a/harness/codeverse/models/gemini_convert.py b/harness/codeverse/models/gemini_convert.py
index e3505415..b4c1f191 100644
--- a/harness/codeverse/models/gemini_convert.py
+++ b/harness/codeverse/models/gemini_convert.py
@@ -8,7 +8,6 @@ from __future__ import annotations
import json
import threading
-from collections import OrderedDict
from typing import Any
from google.genai import types
@@ -24,7 +23,7 @@ from codeverse.contracts.chat import (
)
from codeverse.contracts.common import Usage
from codeverse.models.base import ModelError
-from codeverse.models.parts import image_bytes
+from codeverse.models.parts import BoundedCache, image_bytes
from codeverse.models.schema_utils import to_gemini_schema
#: INTERFACES.md mapping; ``off`` → 0 where the model allows it
@@ -36,31 +35,10 @@ RETRYABLE_FINISH = {"RECITATION", "MALFORMED_FUNCTION_CALL", "OTHER", "UNEXPECTE
FATAL_FINISH = {"SAFETY", "PROHIBITED_CONTENT", "BLOCKLIST", "SPII", "IMAGE_SAFETY"}
-class _SignatureCache:
- """Gemini 3 function calls carry an opaque ``thought_signature`` that MUST be
- echoed back with the call on the next turn. ``ToolCallPart`` has no slot for
- it, so we remember signatures by our synthetic call id (bounded LRU)."""
-
- def __init__(self, capacity: int = 4096) -> None:
- self._d: OrderedDict[str, bytes] = OrderedDict()
- self._cap = capacity
- self._lock = threading.Lock()
-
- def put(self, call_id: str, sig: bytes | None) -> None:
- if not sig:
- return
- with self._lock:
- self._d[call_id] = sig
- self._d.move_to_end(call_id)
- while len(self._d) > self._cap:
- self._d.popitem(last=False)
-
- def get(self, call_id: str) -> bytes | None:
- with self._lock:
- return self._d.get(call_id)
-
-
-SIGNATURES = _SignatureCache()
+#: Gemini 3 function calls carry an opaque ``thought_signature`` that MUST be
+#: echoed back with the call on the next turn. ``ToolCallPart`` has no slot for
+#: it, so we remember signatures by our synthetic call id (bounded LRU).
+SIGNATURES: BoundedCache[bytes] = BoundedCache(4096)
_call_counter = [0]
_call_lock = threading.Lock()
diff --git a/harness/codeverse/models/parts.py b/harness/codeverse/models/parts.py
index 4619e66f..4d45d1d4 100644
--- a/harness/codeverse/models/parts.py
+++ b/harness/codeverse/models/parts.py
@@ -1,10 +1,13 @@
-"""Small helpers shared by the provider backends (images, timing, usage)."""
+"""Small helpers shared by the provider backends (images, timing, usage,
+bounded per-call-id cache)."""
from __future__ import annotations
import base64
import mimetypes
+import threading
import time
+from collections import OrderedDict
from pathlib import Path
from codeverse.contracts.chat import ImagePart
@@ -58,3 +61,34 @@ class Stopwatch:
def __exit__(self, *exc: object) -> None:
self.ms = int((time.perf_counter() - self._t0) * 1000)
+
+
+class BoundedCache[T]:
+ """Thread-safe bounded LRU ``str -> T`` map for per-call-id provider state.
+
+ Replaces ``gemini_convert._SignatureCache`` (thought signatures, ``bytes``)
+ and ``anthropic_convert._ThinkingCache`` (thinking blocks, ``list[dict]``):
+ both had the identical ``__init__``/``put``/``get`` — an ``OrderedDict``
+ under a lock, ``put`` skipping falsy values, ``move_to_end`` then
+ ``popitem(last=False)`` until under capacity — and differed only in value
+ type and capacity. ``get`` returns ``None`` on a miss; a caller that needs
+ the old copy/empty-default shape wraps it: ``list(cache.get(k) or [])``.
+ """
+
+ def __init__(self, capacity: int) -> None:
+ self._d: OrderedDict[str, T] = OrderedDict()
+ self._cap = capacity
+ self._lock = threading.Lock()
+
+ def put(self, key: str, value: T | None) -> None:
+ if not value:
+ return
+ with self._lock:
+ self._d[key] = value
+ self._d.move_to_end(key)
+ while len(self._d) > self._cap:
+ self._d.popitem(last=False)
+
+ def get(self, key: str) -> T | None:
+ with self._lock:
+ return self._d.get(key)