A conversation lives in two places - ai->chat and the KV cache - and prev_len
says how much of the rendered transcript is already in that cache. Nothing
maintained the pairing, so five defects fell out of the same invariant.
llm_chat_free() reset prev_len and dropped the messages but left the cache full,
so the obvious recovery from a full context - llm_chat_free() then
llm_chat_create() - failed with the very same "Context size exceeded", the new
chat having inherited 254 tokens from the old one. It now clears the cache;
llm_chat_create() and llm_chat_restore() both route through it and both wanted
that already.
llm_context_free() left prev_len pointing at a context that no longer existed, so
freeing and recreating appeared to work while the model silently lost the
conversation: 93 tokens of transcript before the resize, 33 after the next turn.
prev_len belongs to the context, so freeing it makes the answer zero and the next
turn re-primes the whole transcript. The history is deliberately kept - that is
what separates freeing a context from freeing a chat, and it is what lets a
context be resized mid-conversation.
ai_free() bypassed both, and llm_model_free() and llm_model_load() go through it.
llm_context_create_with_options() only calls llm_context_free() when ai->ctx is
non-NULL, so after a model free the reset was skipped entirely: 70 tokens before
a reload, 28 after. Rather than a third copy of the reset, each teardown now has
one definition - ai_context_release() and ai_chat_release() - and all four
callers route through them. A context was destroyed in two places and a chat in
two places with only one of each maintaining the invariant; that duplication was
the actual defect.
llm_chat_free() was also the only thing that ever released the history, buffers,
prompt and token array, and it is a SQL function a caller may never invoke.
Closing a connection leaked 29792 bytes. ai_free() now releases the chat when the
connection goes away. Every existing test called llm_chat_free() explicitly,
which is why nothing caught it.
Resetting prev_len makes a replayed transcript a single batch, and llama_decode()
does not return an error when that exceeds n_batch - llama-context.cpp:1487 is
GGML_ASSERT(n_tokens_all <= cparams.n_batch), so it aborts the process. Only the
context_size key ties n_batch to n_ctx, so n_ctx alone leaves n_batch at llama's
default of 2048 and a single ~3000 token prompt is enough. Pre-existing, but the
reset widens it considerably. The chat path now chunks by n_batch the way
llm_text_run() already did and llm_embed_generate_run() already clamped for.
A conversation deliberately survives a model switch: the next turn replays it
through the new model's template, which also sidesteps the byte-offset mismatch
between two templates since the offset is zero. Dropping it would destroy user
data as a side effect of loading a model. Call llm_chat_free() first for a clean
slate.
Six regression tests, each verified to fail against the previous build. Two
changes are deliberately untested - zeroing chat.batch and token_count is
unobservable, since every read is preceded by a write that overwrites them.
Documents all of it: API.md gains a Lifecycle section covering what each *_free()
releases, what survives a model switch, and the three ways out of a full context.
Also corrects the API.md errors recorded in notes/DEMO-FINDINGS.md - the
embedding_type spelling is FLOATB16 not BFLOAT16 (and an unrecognised name
reports "must be specified", so the docs sent callers looking for the wrong
problem), json_output returns a JSON array rather than an object,
llm_chat_restore() returns the message count rather than NULL - and the "funtion"
typo in the error message the docs now quote.
Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_012KRojTrn4Q4SaZQpqcwpAt