Commit f76f479
fix(chat-cache): second audit pass — 9 more bugs eliminated
Follow-up to PR #52. A fresh code-reading audit found another batch of
hidden bugs in the chat KV cache path. None had visible symptoms in the
happy path; all were latent failure modes that would surface under load,
on long histories, or on memory pressure.
Bugs fixed:
CRITICAL
- B1: tq_generate_continue's sliding-window truncation silently desynced
cached_text. cached_text claimed the FULL prompt was committed, but
cached_tokens only held the truncated tail — next turn's text-prefix
match mapped text bytes to the wrong KV positions. Fix: continue now
returns -2 on overflow instead of truncating.
- B2: cached_text was updated even when generation returned an error,
leaving the cache claiming committed bytes that weren't.
- B3: chat_accum_callback realloc failure silently dropped tokens AND
skipped the user's stream callback — broken UX + corrupted cached_text.
Fix: always pass tokens to user_cb; mark accumulator tainted on
realloc failure; skip cached_text update if tainted.
- B4: server's get_or_create_session didn't NULL-check tq_create_state_ex.
An OOM made the next call dereference a NULL kv_state.
- B5: CLI cmd_run interactive loop ignored quant_chat return code, so
context overflow produced an infinite stream of empty replies. Fix:
catch ChatContextOverflow, drop oldest turn, retry.
HIGH
- B6: server streaming path only branched on rc == -2; rc == -1 produced
HTTP 200 with finish_reason "stop" and no error info. Now sends an
error delta + finish_reason "error".
- B7: server reused an existing session even when the request changed
kv_type / value_quant_bits — old quantized blocks would be
misinterpreted. Now detects the change and rebuilds state.
- B8: WASM wasm_load_model didn't reset g_generating. After a
page-reload mid-stream, every subsequent generate call early-returned
-1 forever.
- B9: rep_penalty was silently ignored in tq_generate_chat_text's FAST
path (slow path applied it). Now mirrors the slow path.
- B10: Python Model.chat() ignored the C return value; -2 / -1 surfaced
as empty token streams. Now raises ChatContextOverflow / RuntimeError.
MEDIUM
- Removed dead `update_cache:` label.
- Synced bindings/python/quant.h (sdist staging snapshot).
Verification:
- ctest --test-dir build → 35/35 passed
- cmake --build build → all targets clean
- wasm/build.sh → quant.wasm rebuilt clean (320K)
- Python imports + cli --help work
quant.h and src/engine/tq_generate.c are kept in lockstep (every
chat-cache change applied to both).
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>1 parent 05b7e11 commit f76f479
File tree
8 files changed
+1056
-222
lines changed- bindings/python
- quantcpp
- src
- engine
- server
- wasm
8 files changed
+1056
-222
lines changedLarge diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
35 | 35 | | |
36 | 36 | | |
37 | 37 | | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
38 | 48 | | |
39 | 49 | | |
40 | 50 | | |
| |||
394 | 404 | | |
395 | 405 | | |
396 | 406 | | |
| 407 | + | |
| 408 | + | |
| 409 | + | |
| 410 | + | |
| 411 | + | |
| 412 | + | |
| 413 | + | |
| 414 | + | |
| 415 | + | |
397 | 416 | | |
398 | 417 | | |
399 | 418 | | |
| |||
414 | 433 | | |
415 | 434 | | |
416 | 435 | | |
| 436 | + | |
417 | 437 | | |
418 | 438 | | |
419 | 439 | | |
| |||
424 | 444 | | |
425 | 445 | | |
426 | 446 | | |
427 | | - | |
| 447 | + | |
| 448 | + | |
428 | 449 | | |
429 | 450 | | |
430 | 451 | | |
| |||
448 | 469 | | |
449 | 470 | | |
450 | 471 | | |
| 472 | + | |
| 473 | + | |
| 474 | + | |
| 475 | + | |
| 476 | + | |
| 477 | + | |
| 478 | + | |
| 479 | + | |
| 480 | + | |
| 481 | + | |
| 482 | + | |
| 483 | + | |
| 484 | + | |
451 | 485 | | |
452 | 486 | | |
453 | 487 | | |
| |||
528 | 562 | | |
529 | 563 | | |
530 | 564 | | |
531 | | - | |
| 565 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
151 | 151 | | |
152 | 152 | | |
153 | 153 | | |
| 154 | + | |
154 | 155 | | |
155 | 156 | | |
156 | 157 | | |
157 | 158 | | |
158 | | - | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
159 | 170 | | |
160 | 171 | | |
161 | 172 | | |
162 | 173 | | |
163 | 174 | | |
164 | | - | |
165 | 175 | | |
166 | 176 | | |
167 | | - | |
168 | | - | |
169 | | - | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
170 | 208 | | |
171 | | - | |
| 209 | + | |
| 210 | + | |
172 | 211 | | |
173 | 212 | | |
174 | 213 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
15695 | 15695 | | |
15696 | 15696 | | |
15697 | 15697 | | |
15698 | | - | |
15699 | | - | |
15700 | | - | |
| 15698 | + | |
| 15699 | + | |
| 15700 | + | |
| 15701 | + | |
| 15702 | + | |
| 15703 | + | |
| 15704 | + | |
15701 | 15705 | | |
15702 | 15706 | | |
15703 | 15707 | | |
15704 | 15708 | | |
15705 | | - | |
15706 | | - | |
15707 | | - | |
15708 | | - | |
| 15709 | + | |
| 15710 | + | |
| 15711 | + | |
| 15712 | + | |
| 15713 | + | |
| 15714 | + | |
15709 | 15715 | | |
15710 | 15716 | | |
15711 | 15717 | | |
| |||
15872 | 15878 | | |
15873 | 15879 | | |
15874 | 15880 | | |
| 15881 | + | |
15875 | 15882 | | |
15876 | 15883 | | |
15877 | 15884 | | |
15878 | 15885 | | |
15879 | 15886 | | |
15880 | 15887 | | |
15881 | 15888 | | |
| 15889 | + | |
| 15890 | + | |
| 15891 | + | |
| 15892 | + | |
| 15893 | + | |
15882 | 15894 | | |
15883 | 15895 | | |
15884 | 15896 | | |
15885 | 15897 | | |
15886 | | - | |
| 15898 | + | |
15887 | 15899 | | |
15888 | 15900 | | |
15889 | 15901 | | |
15890 | 15902 | | |
15891 | 15903 | | |
15892 | 15904 | | |
15893 | | - | |
15894 | 15905 | | |
15895 | 15906 | | |
15896 | 15907 | | |
| |||
15918 | 15929 | | |
15919 | 15930 | | |
15920 | 15931 | | |
15921 | | - | |
| 15932 | + | |
15922 | 15933 | | |
15923 | 15934 | | |
15924 | 15935 | | |
| |||
15982 | 15993 | | |
15983 | 15994 | | |
15984 | 15995 | | |
15985 | | - | |
| 15996 | + | |
| 15997 | + | |
15986 | 15998 | | |
15987 | 15999 | | |
15988 | 16000 | | |
15989 | 16001 | | |
15990 | 16002 | | |
| 16003 | + | |
| 16004 | + | |
| 16005 | + | |
| 16006 | + | |
| 16007 | + | |
| 16008 | + | |
| 16009 | + | |
| 16010 | + | |
| 16011 | + | |
| 16012 | + | |
| 16013 | + | |
| 16014 | + | |
| 16015 | + | |
| 16016 | + | |
| 16017 | + | |
| 16018 | + | |
| 16019 | + | |
| 16020 | + | |
| 16021 | + | |
| 16022 | + | |
| 16023 | + | |
| 16024 | + | |
15991 | 16025 | | |
15992 | 16026 | | |
15993 | 16027 | | |
| |||
16033 | 16067 | | |
16034 | 16068 | | |
16035 | 16069 | | |
| 16070 | + | |
| 16071 | + | |
| 16072 | + | |
| 16073 | + | |
| 16074 | + | |
| 16075 | + | |
| 16076 | + | |
| 16077 | + | |
| 16078 | + | |
| 16079 | + | |
| 16080 | + | |
| 16081 | + | |
| 16082 | + | |
16036 | 16083 | | |
16037 | 16084 | | |
16038 | 16085 | | |
| 16086 | + | |
| 16087 | + | |
16039 | 16088 | | |
16040 | 16089 | | |
16041 | 16090 | | |
| |||
16051 | 16100 | | |
16052 | 16101 | | |
16053 | 16102 | | |
16054 | | - | |
16055 | 16103 | | |
16056 | 16104 | | |
16057 | 16105 | | |
| 16106 | + | |
| 16107 | + | |
| 16108 | + | |
| 16109 | + | |
| 16110 | + | |
| 16111 | + | |
| 16112 | + | |
16058 | 16113 | | |
16059 | | - | |
16060 | | - | |
16061 | | - | |
16062 | | - | |
16063 | | - | |
16064 | | - | |
16065 | | - | |
16066 | | - | |
16067 | | - | |
16068 | | - | |
| 16114 | + | |
| 16115 | + | |
| 16116 | + | |
| 16117 | + | |
| 16118 | + | |
| 16119 | + | |
| 16120 | + | |
| 16121 | + | |
| 16122 | + | |
| 16123 | + | |
| 16124 | + | |
| 16125 | + | |
| 16126 | + | |
| 16127 | + | |
| 16128 | + | |
| 16129 | + | |
| 16130 | + | |
| 16131 | + | |
16069 | 16132 | | |
16070 | 16133 | | |
16071 | 16134 | | |
| |||
0 commit comments