ion7-llm / kv

class

ion7.llm.kv.ContextManager

_ctx ion7.core.Context
_vocab ion7.core.Vocab
_slots ion7.llm.kv.Slots
_prefix ion7.llm.kv.Prefix
_hooks table
_opts table

Functions

ContextManager.new

Build a manager.

ContextManager.new(ctx, vocab, opts)
ctxion7.core.Context
vocabion7.core.Vocab
optstable?
→ ion7.llm.kv.ContextManager

ContextManager:set_hook

Install a hook. See module docstring for the list of names.

ContextManager:set_hook(name, fn)
namestring
fnfunction

ContextManager:set_system

Set or replace the system prompt. Does the right thing for both multi-seq (encode into the reserved prefix slot once, copy to every session) and single-seq (remember text only) modes. A change in system text invalidates the radix cache : every stored blob was taken under the previous system, restoring it now would mix two different priors.

ContextManager:set_system(text)
textstring
→ integer

ContextManager:prepare

Make `ctx` ready for the next decode of `session`. After this call the session's row contains exactly the encoded prefix + conversation history up to (but not including) the next reply.

ContextManager:prepare(session)
sessionion7.llm.Session
→ integern_past after prepare (also written onto session)

ContextManager:make_room

Reclaim at least `n_needed` cells in `session`'s KV row by evicting old tokens. Used by the engine and pool to push back the n_ctx wall mid-generation without re-rendering the prompt. The session's `n_past` is updated in place ; the caller must keep its sampler state coherent across the call (penalty history is window-relative, so a small post-eviction off-by-one is tolerable in practice — a deliberate eviction means the model is already at the edge of its useful context anyway).

ContextManager:make_room(session, n_needed)
sessionion7.llm.Session
n_neededintegerMinimum cells to free.
→ integerTokens actually freed (may exceed `n_needed`,

ContextManager:release

Drop the session's KV row + return its slot to the pool. Call when a conversation is permanently closed.

ContextManager:release(session)
sessionion7.llm.Session

ContextManager:fork

Fork a session : duplicate its KV row onto a fresh slot. The child's seq_id is assigned here ; on first prepare the child will snapshot from the copy, no prefix re-encoding needed.

ContextManager:fork(session)
sessionion7.llm.Session
→ ion7.llm.Session

ContextManager:stats

Snapshot of the manager's runtime stats.

ContextManager:stats()
→ table

ContextManager:clear_radix

Force-clear the radix cache. Useful between unrelated workloads when blobs from prior runs would no longer be useful.

ContextManager:clear_radix()