Commit 86d9bd7
perf(memory): cache entry token_count to skip re-tokenization on every render (#939)
* perf(memory): cache entry token_count to skip re-tokenization on every render
Persona / reflection entries now carry two derived fields:
- token_count: int | None
- token_count_text_sha256: str | None
On render, PersonaManager._ascore_trim_entries computes sha256(text) and
reuses the cached count on fingerprint match. Otherwise it calls
acount_tokens once and writes both fields back in-memory. The cache rides
along on normal persona/reflection saves (no new event type). Fresh boot
re-tokenizes on first render.
amerge_into explicitly invalidates the cache when rewriting text to
avoid the tiny window where a concurrent reader might see new text +
stale count; the fingerprint check would catch it otherwise.
Follow-up optimization to PR #936 (render budget): ~200ms saved per
render for 100 entries once the cache is warm.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
* fix(memory/tokenize): key token_count cache by tokenizer identity, not just text
Round-1 review on PR #939 (codex, P2) flagged a correctness gap: the
cache keyed only on sha256(text), but `utils.tokenize.count_tokens`
silently falls back from tiktoken to a heuristic when the encoding data
file is unavailable (packaging products without o200k_base.tiktoken).
A cache warmed under tiktoken then served to a heuristic-fallback
render would return a count off by ~1.5-2×, making the render budget
trim arbitrary.
Fix: add a third cache field `token_count_tokenizer` populated from a
new `utils.tokenize.tokenizer_identity()` helper that returns
`tiktoken:<encoding>` or `heuristic:<version>` based on the current
encoder cache state. Cache hit requires BOTH text sha256 AND tokenizer
identity to match; mismatch on either triggers recompute and re-stamp.
`_invalidate_token_count_cache` now clears all three fields.
`_normalize_entry` / `_normalize_reflection` default the new field to
None so legacy on-disk data reads as a cache miss on first render.
Regression tests cover:
- sync identity mismatch → recompute
- async identity mismatch → recompute (render hot path)
- tiktoken → heuristic transition (real failure mode, counts differ)
- tokenizer_identity() output shape is stable
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
* fix(memory/tokencache): invalidate on card-sync rewrite + scope cache to persona-only
PR #939 round-2 review (CodeRabbit):
1. Minor — `_apply_character_card_sync` (persona.py:512) rewrites
`entry['text']` in place when a character card field changes, but
never called `_invalidate_token_count_cache(entry)` like `amerge_into`
does. The fingerprint check would still catch the drift on the next
render, so results were correct — but one extra sha256 per stale
entry, and the intent was hidden. Added the invalidator call,
mirroring the `amerge_into` pattern.
2. Major — reflection token-count cache was architecturally ineffective.
Verified in reflection.py: `aload_reflections` and
`_aload_reflections_full` both read fresh from disk every call —
there is no `self._reflections` process-resident view mirroring
`PersonaManager._personas`. Any cache writeback on a reflection entry
lived on a transient list that got garbage-collected on render exit,
so the "cache" was doing the work without ever serving a hit.
Chose Option A (be honest): drop the reflection cache entirely rather
than grow an in-memory view (bigger refactor, beyond this PR's perf-
optimization scope) or persist derived fields on render (bypasses the
event-log contract). Persona-side caching still captures the bulk of
render time in typical workloads.
Implementation:
- `_normalize_reflection` no longer defaults the three `token_count*`
fields. Legacy reflections that already carry the fields are
preserved byte-for-byte (no data loss on existing on-disk state).
- `_get_cached_token_count` / `_aget_cached_token_count` gained a
`writeback=True` kwarg. When False, we still short-circuit on a
pre-existing cache hit (free) but never mutate the entry.
- `_score_trim_entries` / `_ascore_trim_entries` gained a
`cache_writeback` kwarg, forwarded to the token-count helper.
- Both reflection call sites in `arender_persona_markdown` +
`_compose_persona_markdown` now pass `cache_writeback=False`, so
the render math still works without polluting reflection.json.
Tests:
- Added `test_character_card_sync_invalidates_cache_on_text_rewrite`
(regression guard for fix #1).
- Replaced `test_cache_survives_reflection_save_reload_roundtrip` with
`test_reflection_render_does_not_pollute_cache_fields` (locks in the
new contract: render + save round-trip leaves cache fields absent).
- Replaced `test_normalize_reflection_defaults_cache_fields_to_none`
with `test_normalize_reflection_does_not_default_cache_fields` (also
verifies legacy on-disk entries that happen to carry the fields
aren't silently dropped).
All 64 targeted tests green. Broader tests/unit/ suite: 950 passed, the
2 failures + 2 errors are pre-existing on the branch tip before this
commit (verified via `git stash` + rerun).
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
* fix(memory/tokencache): defensive int validation on cache hit to survive poisoned disk
The hit path previously did `int(entry['token_count'])` unconditionally,
trusting whatever came off disk. A hand-edited or corrupted persona.json
could plant a non-numeric string ("??"), null, boolean, or negative value
while the sha256+tokenizer fingerprints still match — in which case the
bare int() either raised (bombed the render) or returned meaningless
garbage (blew the budget math).
New `_coerce_cached_count` helper validates the cached value: returns the
non-negative int on success, None on None/non-numeric/boolean/negative so
the hit branch falls through to recompute. Booleans are rejected
explicitly (bool is an int subclass, so `int(True) == 1` would otherwise
silently succeed). Applies to both sync and async twins.
Regression tests cover non-numeric, negative, null, and boolean poisoned
values on both render paths, plus direct unit coverage of the coercion
helper shape.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
* fix(memory/tokencache): reject non-integer floats and inf/nan in cache coerce
_coerce_cached_count previously used bare int(raw), which silently
truncated 1.9 → 1 (a poisoned cache value matching fingerprint would
make the render budget slightly off) and let float('inf') escape as
an uncaught OverflowError mid-render. Reject both: non-integer floats
(via .is_integer()) and any float overflow / TypeError / ValueError /
OverflowError → cache miss + recompute.
Reported by coderabbitai on PR #939.
* docs(memory/tokencache): fix reflection contract misstatement in cache comment
The module-level comment block claimed both _normalize_entry and
_normalize_reflection default the three token_count* fields to None.
In practice _normalize_reflection deliberately does NOT — reflections
skip the defaults because their renders use writeback=False (no
in-memory cache to persist into). Rewrite the comment to state each
side's contract accurately.
---------
Co-authored-by: Hongzhi Wen <cartabio.coder1@gmail.com>
Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>1 parent 360e3aa commit 86d9bd7
4 files changed
Lines changed: 1185 additions & 7 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
41 | 41 | | |
42 | 42 | | |
43 | 43 | | |
44 | | - | |
| 44 | + | |
45 | 45 | | |
46 | 46 | | |
47 | 47 | | |
| |||
510 | 510 | | |
511 | 511 | | |
512 | 512 | | |
| 513 | + | |
| 514 | + | |
| 515 | + | |
| 516 | + | |
| 517 | + | |
513 | 518 | | |
514 | 519 | | |
515 | 520 | | |
| |||
662 | 667 | | |
663 | 668 | | |
664 | 669 | | |
| 670 | + | |
| 671 | + | |
| 672 | + | |
| 673 | + | |
| 674 | + | |
| 675 | + | |
| 676 | + | |
| 677 | + | |
| 678 | + | |
| 679 | + | |
| 680 | + | |
| 681 | + | |
| 682 | + | |
| 683 | + | |
| 684 | + | |
665 | 685 | | |
666 | 686 | | |
667 | 687 | | |
| |||
684 | 704 | | |
685 | 705 | | |
686 | 706 | | |
| 707 | + | |
| 708 | + | |
| 709 | + | |
| 710 | + | |
| 711 | + | |
| 712 | + | |
| 713 | + | |
| 714 | + | |
| 715 | + | |
| 716 | + | |
687 | 717 | | |
688 | 718 | | |
689 | 719 | | |
| |||
1122 | 1152 | | |
1123 | 1153 | | |
1124 | 1154 | | |
| 1155 | + | |
| 1156 | + | |
| 1157 | + | |
| 1158 | + | |
| 1159 | + | |
| 1160 | + | |
| 1161 | + | |
1125 | 1162 | | |
1126 | 1163 | | |
1127 | 1164 | | |
| |||
1742 | 1779 | | |
1743 | 1780 | | |
1744 | 1781 | | |
| 1782 | + | |
| 1783 | + | |
| 1784 | + | |
| 1785 | + | |
| 1786 | + | |
| 1787 | + | |
| 1788 | + | |
| 1789 | + | |
| 1790 | + | |
| 1791 | + | |
| 1792 | + | |
| 1793 | + | |
| 1794 | + | |
| 1795 | + | |
| 1796 | + | |
| 1797 | + | |
| 1798 | + | |
| 1799 | + | |
| 1800 | + | |
| 1801 | + | |
| 1802 | + | |
| 1803 | + | |
| 1804 | + | |
| 1805 | + | |
| 1806 | + | |
| 1807 | + | |
| 1808 | + | |
| 1809 | + | |
| 1810 | + | |
| 1811 | + | |
| 1812 | + | |
| 1813 | + | |
| 1814 | + | |
| 1815 | + | |
| 1816 | + | |
| 1817 | + | |
| 1818 | + | |
| 1819 | + | |
| 1820 | + | |
| 1821 | + | |
| 1822 | + | |
| 1823 | + | |
| 1824 | + | |
| 1825 | + | |
| 1826 | + | |
| 1827 | + | |
| 1828 | + | |
| 1829 | + | |
| 1830 | + | |
| 1831 | + | |
| 1832 | + | |
| 1833 | + | |
| 1834 | + | |
| 1835 | + | |
| 1836 | + | |
| 1837 | + | |
| 1838 | + | |
| 1839 | + | |
| 1840 | + | |
| 1841 | + | |
| 1842 | + | |
| 1843 | + | |
| 1844 | + | |
| 1845 | + | |
| 1846 | + | |
| 1847 | + | |
| 1848 | + | |
| 1849 | + | |
| 1850 | + | |
| 1851 | + | |
| 1852 | + | |
| 1853 | + | |
| 1854 | + | |
| 1855 | + | |
| 1856 | + | |
| 1857 | + | |
| 1858 | + | |
| 1859 | + | |
| 1860 | + | |
| 1861 | + | |
| 1862 | + | |
| 1863 | + | |
| 1864 | + | |
| 1865 | + | |
| 1866 | + | |
| 1867 | + | |
| 1868 | + | |
| 1869 | + | |
| 1870 | + | |
| 1871 | + | |
| 1872 | + | |
| 1873 | + | |
| 1874 | + | |
| 1875 | + | |
| 1876 | + | |
| 1877 | + | |
| 1878 | + | |
| 1879 | + | |
| 1880 | + | |
| 1881 | + | |
| 1882 | + | |
| 1883 | + | |
| 1884 | + | |
| 1885 | + | |
| 1886 | + | |
| 1887 | + | |
| 1888 | + | |
| 1889 | + | |
| 1890 | + | |
| 1891 | + | |
| 1892 | + | |
| 1893 | + | |
| 1894 | + | |
| 1895 | + | |
1745 | 1896 | | |
| 1897 | + | |
| 1898 | + | |
| 1899 | + | |
| 1900 | + | |
| 1901 | + | |
| 1902 | + | |
| 1903 | + | |
| 1904 | + | |
| 1905 | + | |
| 1906 | + | |
| 1907 | + | |
| 1908 | + | |
| 1909 | + | |
| 1910 | + | |
| 1911 | + | |
| 1912 | + | |
| 1913 | + | |
| 1914 | + | |
| 1915 | + | |
| 1916 | + | |
| 1917 | + | |
| 1918 | + | |
| 1919 | + | |
| 1920 | + | |
| 1921 | + | |
| 1922 | + | |
| 1923 | + | |
| 1924 | + | |
| 1925 | + | |
| 1926 | + | |
| 1927 | + | |
| 1928 | + | |
| 1929 | + | |
| 1930 | + | |
| 1931 | + | |
| 1932 | + | |
| 1933 | + | |
| 1934 | + | |
| 1935 | + | |
| 1936 | + | |
| 1937 | + | |
| 1938 | + | |
1746 | 1939 | | |
1747 | | - | |
| 1940 | + | |
| 1941 | + | |
1748 | 1942 | | |
1749 | 1943 | | |
1750 | 1944 | | |
| |||
1753 | 1947 | | |
1754 | 1948 | | |
1755 | 1949 | | |
| 1950 | + | |
| 1951 | + | |
| 1952 | + | |
| 1953 | + | |
| 1954 | + | |
| 1955 | + | |
| 1956 | + | |
1756 | 1957 | | |
1757 | 1958 | | |
1758 | 1959 | | |
| |||
1765 | 1966 | | |
1766 | 1967 | | |
1767 | 1968 | | |
1768 | | - | |
| 1969 | + | |
1769 | 1970 | | |
1770 | 1971 | | |
1771 | 1972 | | |
1772 | 1973 | | |
1773 | 1974 | | |
1774 | 1975 | | |
1775 | | - | |
| 1976 | + | |
1776 | 1977 | | |
1777 | | - | |
| 1978 | + | |
| 1979 | + | |
1778 | 1980 | | |
1779 | 1981 | | |
1780 | | - | |
| 1982 | + | |
| 1983 | + | |
1781 | 1984 | | |
1782 | 1985 | | |
1783 | 1986 | | |
| |||
1789 | 1992 | | |
1790 | 1993 | | |
1791 | 1994 | | |
1792 | | - | |
| 1995 | + | |
1793 | 1996 | | |
1794 | 1997 | | |
1795 | 1998 | | |
| |||
1993 | 2196 | | |
1994 | 2197 | | |
1995 | 2198 | | |
| 2199 | + | |
| 2200 | + | |
| 2201 | + | |
| 2202 | + | |
| 2203 | + | |
1996 | 2204 | | |
1997 | 2205 | | |
1998 | 2206 | | |
| |||
2089 | 2297 | | |
2090 | 2298 | | |
2091 | 2299 | | |
| 2300 | + | |
| 2301 | + | |
| 2302 | + | |
| 2303 | + | |
2092 | 2304 | | |
2093 | 2305 | | |
2094 | 2306 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
191 | 191 | | |
192 | 192 | | |
193 | 193 | | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
194 | 208 | | |
195 | 209 | | |
196 | 210 | | |
| |||
0 commit comments