From 9cdf9f5d4d7d3f7e8a74bcb5965974c19722b249 Mon Sep 17 00:00:00 2001 From: Joel Teply Date: Wed, 18 Nov 2026 18:08:25 -0600 Subject: [PATCH 1/4] llama: a persona checkpoint rewinds her to a stable boundary without moving her (CONTINUUM-ENGINE 4.15) A mind re-prefilled her whole prompt every act on the M5 (2026-11-18, reused 0 of 11k-30k tokens per act): her prompt is append-only up to a volatile trailer, but a hybrid's recurrent state cannot go back, so nothing she held could be reused once the trailer changed. llama_persona_checkpoint_take copies a BOUND sequence's recurrent rows (every snapshot group), position and rollback index, without moving her; her attention KV is not copied. llama_persona_checkpoint_rewind writes the rows back and cuts what she holds past the checkpoint (seq_rm on attention), so the next decode continues from the boundary. Two-phase: a refused rewind leaves her as she was; a refused take leaves no checkpoint; a rewind never moves forward (checkpoint_ahead); one checkpoint rewinds any number of times. test-persona-blocks case 8: a different continuation after a rewind is byte-identical to decoding it in place, twice, on the plain KV model, the hybrid, and the hybrid with rollback groups. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01LoTjvf5j3Ez13g6k8mRkFo --- include/llama.h | 14 ++++ src/llama-context.cpp | 65 +++++++++++++++++++ src/llama-context.h | 2 + src/llama-kv-cache.cpp | 32 +++++++++ src/llama-kv-cache.h | 4 ++ src/llama-memory-hybrid.cpp | 25 ++++++++ src/llama-memory-hybrid.h | 4 ++ src/llama-memory-recurrent.cpp | 114 +++++++++++++++++++++++++++++++++ src/llama-memory-recurrent.h | 4 ++ src/llama-memory.cpp | 2 + src/llama-memory.h | 16 +++++ src/llama-persona-blocks.h | 14 ++++ tests/test-persona-blocks.cpp | 44 +++++++++++++ 13 files changed, 340 insertions(+) diff --git a/include/llama.h b/include/llama.h index 11c03a6ae0a7..c602ccef75e9 100644 --- a/include/llama.h +++ b/include/llama.h @@ -952,6 +952,20 @@ extern "C" { LLAMA_API const char * llama_persona_status_name(int32_t status); + // A checkpoint of a BOUND sequence at its current position, taken without moving her: her recurrent rows (every + // snapshot group) are copied out, her attention KV stays where it is. A rewind writes the rows back and cuts what she + // holds past the checkpoint's position, so the next decode continues from there. Both are two-phase: a refused + // rewind leaves her exactly as she was; a refused take leaves no checkpoint (a later rewind refuses no_checkpoint). + // A rewind never moves forward (checkpoint_ahead). The caller owns WHEN to take one (a stable prompt boundary). + struct llama_persona_checkpoint; + LLAMA_API struct llama_persona_checkpoint * llama_persona_checkpoint_init(void); + LLAMA_API void llama_persona_checkpoint_free(struct llama_persona_checkpoint * checkpoint); + LLAMA_API int32_t llama_persona_checkpoint_take(struct llama_context * ctx, llama_seq_id seq_id, struct llama_persona_checkpoint * out); + LLAMA_API int32_t llama_persona_checkpoint_rewind(struct llama_context * ctx, llama_seq_id seq_id, const struct llama_persona_checkpoint * in); + // The position a taken checkpoint holds through (-1 when none is taken), and the bytes it holds. + LLAMA_API llama_pos llama_persona_checkpoint_pos(const struct llama_persona_checkpoint * checkpoint); + LLAMA_API size_t llama_persona_checkpoint_bytes(const struct llama_persona_checkpoint * checkpoint); + // Her blocks move only under her owner (the persona and epoch she was made for): a completed save stamps them; a // load of blocks stamped by another persona or another epoch of her returns wrong_owner before anything is // written. llama_persona_owner reads the stamp (false before a completed save). diff --git a/src/llama-context.cpp b/src/llama-context.cpp index 872894d66a56..562e2f5e2fbc 100644 --- a/src/llama-context.cpp +++ b/src/llama-context.cpp @@ -2562,6 +2562,36 @@ ggml_cgraph * llama_context::graph_reserve( return gf; } +llama_persona_move llama_context::persona_checkpoint(llama_seq_id seq_id, llama_persona_checkpoint_blocks & out) { + if (!memory) { + return llama_persona_move::unsupported; + } + synchronize(); // the raw copies read device memory a queued decode may still be writing + // Her buffers are reused take after take (no copy of the previous checkpoint). A refused take leaves no checkpoint + // at all, so a later rewind refuses (no_checkpoint) instead of restoring a stale one. + out.taken = false; + const llama_persona_move checked = memory->persona_check_checkpoint(seq_id, out); + if (checked != llama_persona_move::done) { + return checked; + } + memory->persona_commit_checkpoint(seq_id, out); + out.taken = true; + return llama_persona_move::done; +} + +llama_persona_move llama_context::persona_rewind(llama_seq_id seq_id, const llama_persona_checkpoint_blocks & in) { + if (!memory) { + return llama_persona_move::unsupported; + } + synchronize(); + const llama_persona_move checked = memory->persona_check_rewind(seq_id, in); + if (checked != llama_persona_move::done) { + return checked; // refused before anything is written: she stays exactly as she was + } + memory->persona_commit_rewind(seq_id, in); + return llama_persona_move::done; +} + llm_graph_type llama_context::graph_type() const { switch (cparams.ctx_type) { case LLAMA_CONTEXT_TYPE_DEFAULT: return LLM_GRAPH_TYPE_DEFAULT; @@ -4350,6 +4380,41 @@ bool llama_persona_owner(const llama_persona * persona, uint64_t * persona_id, u return true; } +struct llama_persona_checkpoint { + llama_persona_checkpoint_blocks blocks; +}; + +llama_persona_checkpoint * llama_persona_checkpoint_init(void) { + return new (std::nothrow) llama_persona_checkpoint(); +} + +void llama_persona_checkpoint_free(llama_persona_checkpoint * checkpoint) { + delete checkpoint; +} + +int32_t llama_persona_checkpoint_take(llama_context * ctx, llama_seq_id seq_id, llama_persona_checkpoint * out) { + return (int32_t) ctx->persona_checkpoint(seq_id, out->blocks); +} + +int32_t llama_persona_checkpoint_rewind(llama_context * ctx, llama_seq_id seq_id, const llama_persona_checkpoint * in) { + return (int32_t) ctx->persona_rewind(seq_id, in->blocks); +} + +llama_pos llama_persona_checkpoint_pos(const llama_persona_checkpoint * checkpoint) { + return checkpoint->blocks.taken ? checkpoint->blocks.pos : -1; +} + +size_t llama_persona_checkpoint_bytes(const llama_persona_checkpoint * checkpoint) { + size_t bytes = 0; + for (const auto & row : checkpoint->blocks.rs.r) { + bytes += row.size(); + } + for (const auto & row : checkpoint->blocks.rs.s) { + bytes += row.size(); + } + return bytes; +} + const char * llama_persona_status_name(int32_t status) { return llama_persona_move_name((llama_persona_move) status); } diff --git a/src/llama-context.h b/src/llama-context.h index 2f591d621106..7c5488ffb681 100644 --- a/src/llama-context.h +++ b/src/llama-context.h @@ -66,6 +66,8 @@ struct llama_context { llama_persona_move persona_load(llama_seq_id seq_id, uint32_t stream, llama_persona_blocks & in, const llama_persona_identity & who); // load her into an empty stream the memory chooses (no_empty_stream when none is) llama_persona_move persona_bind(llama_seq_id seq_id, llama_persona_blocks & in, const llama_persona_identity & who); + llama_persona_move persona_checkpoint(llama_seq_id seq_id, llama_persona_checkpoint_blocks & out); + llama_persona_move persona_rewind(llama_seq_id seq_id, const llama_persona_checkpoint_blocks & in); bool opt_state_write_initial(FILE * optimizer, const char * binding); bool opt_state_write(FILE * optimizer, const char * binding); bool opt_shadow_state_write(const llama_adapter_lora * shadow, FILE * optimizer, const char * binding); diff --git a/src/llama-kv-cache.cpp b/src/llama-kv-cache.cpp index 41a49ed8b7c2..9d2571e699ad 100644 --- a/src/llama-kv-cache.cpp +++ b/src/llama-kv-cache.cpp @@ -1327,6 +1327,38 @@ void llama_kv_cache::persona_commit_save(llama_seq_id seq_id, llama_persona_bloc v_heads[s] = 0; } +// Attention KV is never copied by a checkpoint: it stays in place, and only her position is recorded, so a rewind can +// cut what she holds past it. +llama_persona_move llama_kv_cache::persona_check_checkpoint(llama_seq_id seq_id, llama_persona_checkpoint_blocks & out) const { + if (seq_id < 0 || (size_t) seq_id >= seq_to_stream.size()) { + return llama_persona_move::invalid_seq; + } + out.pos = v_cells[seq_to_stream[seq_id]].seq_pos_max(seq_id); + return llama_persona_move::done; +} + +void llama_kv_cache::persona_commit_checkpoint(llama_seq_id seq_id, llama_persona_checkpoint_blocks & out) const { + out.seq = seq_id; +} + +llama_persona_move llama_kv_cache::persona_check_rewind(llama_seq_id seq_id, const llama_persona_checkpoint_blocks & in) const { + if (seq_id < 0 || (size_t) seq_id >= seq_to_stream.size()) { + return llama_persona_move::invalid_seq; + } + if (!in.taken) { + return llama_persona_move::no_checkpoint; + } + if (in.pos > v_cells[seq_to_stream[seq_id]].seq_pos_max(seq_id)) { + return llama_persona_move::checkpoint_ahead; + } + return llama_persona_move::done; +} + +void llama_kv_cache::persona_commit_rewind(llama_seq_id seq_id, const llama_persona_checkpoint_blocks & in) { + // what she holds past the checkpoint is cut; what she held up to it is untouched in place + seq_rm(seq_id, in.pos + 1, -1); +} + int32_t llama_kv_cache::persona_empty_stream() const { if (n_stream == 1) { return -1; // unified: one stream holds every sequence, so there is nothing empty to bind into diff --git a/src/llama-kv-cache.h b/src/llama-kv-cache.h index 6e85ed9c25be..71998706311a 100644 --- a/src/llama-kv-cache.h +++ b/src/llama-kv-cache.h @@ -186,6 +186,10 @@ class llama_kv_cache : public llama_memory_i { llama_persona_slot & slot) const override; void persona_commit_save(llama_seq_id seq_id, llama_persona_blocks & out) override; void persona_commit_load(llama_seq_id seq_id, llama_persona_slot & slot, llama_persona_blocks & in) override; + llama_persona_move persona_check_checkpoint(llama_seq_id seq_id, llama_persona_checkpoint_blocks & out) const override; + void persona_commit_checkpoint(llama_seq_id seq_id, llama_persona_checkpoint_blocks & out) const override; + llama_persona_move persona_check_rewind(llama_seq_id seq_id, const llama_persona_checkpoint_blocks & in) const override; + void persona_commit_rewind(llama_seq_id seq_id, const llama_persona_checkpoint_blocks & in) override; uint32_t seq_stream(llama_seq_id seq_id) const override { return seq_to_stream[seq_id]; } diff --git a/src/llama-memory-hybrid.cpp b/src/llama-memory-hybrid.cpp index db115ba6f3ae..9b49502d9795 100644 --- a/src/llama-memory-hybrid.cpp +++ b/src/llama-memory-hybrid.cpp @@ -238,6 +238,31 @@ void llama_memory_hybrid::persona_commit_load(llama_seq_id seq_id, llama_persona mem_recr->persona_commit_load(seq_id, slot, in); } +// Both halves agree on her position or the checkpoint is refused: a rewind must leave attention and recurrent state at +// the same token. +llama_persona_move llama_memory_hybrid::persona_check_checkpoint(llama_seq_id seq_id, llama_persona_checkpoint_blocks & out) const { + const llama_persona_move attn = mem_attn->persona_check_checkpoint(seq_id, out); + return attn != llama_persona_move::done ? attn : mem_recr->persona_check_checkpoint(seq_id, out); +} + +void llama_memory_hybrid::persona_commit_checkpoint(llama_seq_id seq_id, llama_persona_checkpoint_blocks & out) const { + mem_attn->persona_commit_checkpoint(seq_id, out); + mem_recr->persona_commit_checkpoint(seq_id, out); +} + +llama_persona_move llama_memory_hybrid::persona_check_rewind(llama_seq_id seq_id, const llama_persona_checkpoint_blocks & in) const { + if (in.rs.has_state && in.rs.pos != in.pos) { + return llama_persona_move::layout_mismatch; // the halves were not at one token when it was taken + } + const llama_persona_move attn = mem_attn->persona_check_rewind(seq_id, in); + return attn != llama_persona_move::done ? attn : mem_recr->persona_check_rewind(seq_id, in); +} + +void llama_memory_hybrid::persona_commit_rewind(llama_seq_id seq_id, const llama_persona_checkpoint_blocks & in) { + mem_attn->persona_commit_rewind(seq_id, in); + mem_recr->persona_commit_rewind(seq_id, in); +} + llama_kv_cache * llama_memory_hybrid::get_mem_attn() const { return mem_attn.get(); } diff --git a/src/llama-memory-hybrid.h b/src/llama-memory-hybrid.h index 8715212f1d78..ab025fb4696a 100644 --- a/src/llama-memory-hybrid.h +++ b/src/llama-memory-hybrid.h @@ -95,6 +95,10 @@ class llama_memory_hybrid : public llama_memory_i { llama_persona_slot & slot) const override; void persona_commit_save(llama_seq_id seq_id, llama_persona_blocks & out) override; void persona_commit_load(llama_seq_id seq_id, llama_persona_slot & slot, llama_persona_blocks & in) override; + llama_persona_move persona_check_checkpoint(llama_seq_id seq_id, llama_persona_checkpoint_blocks & out) const override; + void persona_commit_checkpoint(llama_seq_id seq_id, llama_persona_checkpoint_blocks & out) const override; + llama_persona_move persona_check_rewind(llama_seq_id seq_id, const llama_persona_checkpoint_blocks & in) const override; + void persona_commit_rewind(llama_seq_id seq_id, const llama_persona_checkpoint_blocks & in) override; uint32_t seq_stream(llama_seq_id seq_id) const override { return mem_attn->seq_stream(seq_id); } llama_memory_recurrent * get_mem_recr() const; diff --git a/src/llama-memory-recurrent.cpp b/src/llama-memory-recurrent.cpp index aec9c96d5693..595e7a864358 100644 --- a/src/llama-memory-recurrent.cpp +++ b/src/llama-memory-recurrent.cpp @@ -551,6 +551,120 @@ void llama_memory_recurrent::persona_commit_load(llama_seq_id seq_id, llama_pers used += 1; } +// A checkpoint copies her recurrent rows (every snapshot group) out of her tail cell and leaves her bound: unlike a +// save, her cell, position and rollback index stay as they are. +llama_persona_move llama_memory_recurrent::persona_check_checkpoint(llama_seq_id seq_id, llama_persona_checkpoint_blocks & out) const { + if (seq_id < 0 || (uint32_t) seq_id >= size || (uint32_t) seq_id >= n_seq_max) { + return llama_persona_move::invalid_seq; + } + const int32_t tail = cells[seq_id].tail; + if (tail >= 0 && cells[tail].seq_id.size() != 1) { + return llama_persona_move::shared_cell; + } + // reserve: every byte the commit copies, so the commit never allocates + const uint32_t n_groups = 1 + n_rs_seq; + out.rs_layout = persona_layout(); + out.rs.r.resize(r_l.size()); + out.rs.s.resize(s_l.size()); + for (size_t il = 0; il < r_l.size(); ++il) { + out.rs.r[il].resize((tail >= 0 && r_l[il]) ? ggml_row_size(r_l[il]->type, r_l[il]->ne[0]) * n_groups : 0); + out.rs.s[il].resize((tail >= 0 && s_l[il]) ? ggml_row_size(s_l[il]->type, s_l[il]->ne[0]) * n_groups : 0); + } + return llama_persona_move::done; +} + +void llama_memory_recurrent::persona_commit_checkpoint(llama_seq_id seq_id, llama_persona_checkpoint_blocks & out) const { + auto & rs = out.rs; + const int32_t tail = cells[seq_id].tail; + rs.has_state = tail >= 0; + out.seq = seq_id; + if (!rs.has_state) { + return; + } + const uint32_t n_groups = 1 + n_rs_seq; + const auto rows_out = [&](const ggml_tensor * src, std::vector & dst) { + if (src == nullptr || dst.empty()) { + return; + } + const size_t row = ggml_row_size(src->type, src->ne[0]); + for (uint32_t g = 0; g < n_groups; ++g) { + ggml_backend_tensor_get(src, dst.data() + g * row, (size_t) (g * size + tail) * src->nb[1], row); + } + }; + for (size_t il = 0; il < r_l.size(); ++il) { + rows_out(r_l[il], rs.r[il]); + rows_out(s_l[il], rs.s[il]); + } + rs.pos = cells[tail].pos; + rs.rs_idx = rs_idx[seq_id]; +} + +llama_persona_move llama_memory_recurrent::persona_check_rewind(llama_seq_id seq_id, const llama_persona_checkpoint_blocks & in) const { + if (seq_id < 0 || (uint32_t) seq_id >= size || (uint32_t) seq_id >= n_seq_max) { + return llama_persona_move::invalid_seq; + } + if (!in.taken) { + return llama_persona_move::no_checkpoint; + } + if (in.rs_layout != persona_layout()) { + return llama_persona_move::layout_mismatch; + } + const auto & rs = in.rs; + const int32_t tail = cells[seq_id].tail; + if (!rs.has_state) { + // a checkpoint of a sequence with no recurrent state rewinds only if she still has none + return tail < 0 ? llama_persona_move::done : llama_persona_move::layout_mismatch; + } + if (tail < 0) { + return llama_persona_move::no_checkpoint; + } + if (cells[tail].seq_id.size() != 1) { + return llama_persona_move::shared_cell; + } + if (rs.pos > cells[tail].pos) { + return llama_persona_move::checkpoint_ahead; + } + if (rs.rs_idx > n_rs_seq) { + return llama_persona_move::invalid_rollback; + } + if (rs.r.size() != r_l.size() || rs.s.size() != s_l.size()) { + return llama_persona_move::layout_mismatch; + } + const uint32_t n_groups = 1 + n_rs_seq; + for (size_t il = 0; il < r_l.size(); ++il) { + const size_t r_bytes = r_l[il] ? ggml_row_size(r_l[il]->type, r_l[il]->ne[0]) * n_groups : 0; + const size_t s_bytes = s_l[il] ? ggml_row_size(s_l[il]->type, s_l[il]->ne[0]) * n_groups : 0; + if (rs.r[il].size() != r_bytes || rs.s[il].size() != s_bytes) { + return llama_persona_move::layout_mismatch; + } + } + return llama_persona_move::done; +} + +void llama_memory_recurrent::persona_commit_rewind(llama_seq_id seq_id, const llama_persona_checkpoint_blocks & in) { + const auto & rs = in.rs; + if (!rs.has_state) { + return; + } + const int32_t tail = cells[seq_id].tail; + const uint32_t n_groups = 1 + n_rs_seq; + const auto rows_in = [&](ggml_tensor * dst, const std::vector & src) { + if (dst == nullptr || src.empty()) { + return; + } + const size_t row = ggml_row_size(dst->type, dst->ne[0]); + for (uint32_t g = 0; g < n_groups; ++g) { + ggml_backend_tensor_set(dst, src.data() + g * row, (size_t) (g * size + tail) * dst->nb[1], row); + } + }; + for (size_t il = 0; il < r_l.size(); ++il) { + rows_in(r_l[il], rs.r[il]); + rows_in(s_l[il], rs.s[il]); + } + cells[tail].pos = rs.pos; + rs_idx[seq_id] = rs.rs_idx; +} + void llama_memory_recurrent::set_rs_idx(llama_seq_id seq_id, uint32_t idx) { if (seq_id < 0) { std::fill(rs_idx.begin(), rs_idx.end(), 0); diff --git a/src/llama-memory-recurrent.h b/src/llama-memory-recurrent.h index 13ac2127614b..e445d583c46c 100644 --- a/src/llama-memory-recurrent.h +++ b/src/llama-memory-recurrent.h @@ -95,6 +95,10 @@ class llama_memory_recurrent : public llama_memory_i { llama_persona_slot & slot) const override; void persona_commit_save(llama_seq_id seq_id, llama_persona_blocks & out) override; void persona_commit_load(llama_seq_id seq_id, llama_persona_slot & slot, llama_persona_blocks & in) override; + llama_persona_move persona_check_checkpoint(llama_seq_id seq_id, llama_persona_checkpoint_blocks & out) const override; + void persona_commit_checkpoint(llama_seq_id seq_id, llama_persona_checkpoint_blocks & out) const override; + llama_persona_move persona_check_rewind(llama_seq_id seq_id, const llama_persona_checkpoint_blocks & in) const override; + void persona_commit_rewind(llama_seq_id seq_id, const llama_persona_checkpoint_blocks & in) override; // an undo of a draft is pending on this sequence: its next read takes a snapshot group, not the // live state (group 0) a training graph reads and writes (llm_graph_context::rs_keep) diff --git a/src/llama-memory.cpp b/src/llama-memory.cpp index 646fb357bed0..00f81a2bb253 100644 --- a/src/llama-memory.cpp +++ b/src/llama-memory.cpp @@ -76,6 +76,8 @@ const char * llama_persona_move_name(llama_persona_move m) { case llama_persona_move::wrong_owner: return "wrong_owner"; case llama_persona_move::out_of_memory: return "out_of_memory"; case llama_persona_move::no_empty_stream: return "no_empty_stream"; + case llama_persona_move::no_checkpoint: return "no_checkpoint"; + case llama_persona_move::checkpoint_ahead: return "checkpoint_ahead"; } return "unknown"; } diff --git a/src/llama-memory.h b/src/llama-memory.h index 5fa23b155c92..a887052e7aa2 100644 --- a/src/llama-memory.h +++ b/src/llama-memory.h @@ -177,6 +177,22 @@ struct llama_memory_i { virtual void persona_commit_load(llama_seq_id seq_id, llama_persona_slot & slot, llama_persona_blocks & in) { (void) seq_id; (void) slot; (void) in; } + // A checkpoint of a BOUND sequence, the same two phases: check reserves every byte (sizes `out`), commit copies her + // rows out and leaves her in place. A rewind writes them back and cuts what she holds past the checkpoint. + virtual llama_persona_move persona_check_checkpoint(llama_seq_id seq_id, llama_persona_checkpoint_blocks & out) const { + (void) seq_id; (void) out; + return llama_persona_move::unsupported; + } + virtual void persona_commit_checkpoint(llama_seq_id seq_id, llama_persona_checkpoint_blocks & out) const { + (void) seq_id; (void) out; + } + virtual llama_persona_move persona_check_rewind(llama_seq_id seq_id, const llama_persona_checkpoint_blocks & in) const { + (void) seq_id; (void) in; + return llama_persona_move::unsupported; + } + virtual void persona_commit_rewind(llama_seq_id seq_id, const llama_persona_checkpoint_blocks & in) { + (void) seq_id; (void) in; + } virtual std::map memory_breakdown() const = 0; diff --git a/src/llama-persona-blocks.h b/src/llama-persona-blocks.h index 24601fb4dce8..5e6eea976656 100644 --- a/src/llama-persona-blocks.h +++ b/src/llama-persona-blocks.h @@ -82,6 +82,20 @@ enum class llama_persona_move { wrong_owner, // her blocks were saved by another persona or another epoch of her out_of_memory, // growing or shrinking her token history failed to allocate; nothing changed no_empty_stream, // a bind found no empty stream to place her in + no_checkpoint, // a rewind was asked of a checkpoint never taken + checkpoint_ahead, // a rewind was asked to a position past what she holds: a rewind never moves forward +}; + +// A copy of a bound persona at one of her positions, taken WITHOUT moving her: her recurrent rows (every snapshot +// group), her recurrent position and rollback index. Her attention KV is not copied: it stays in place, and a rewind +// cuts it back past the checkpoint's position. So a checkpoint costs her recurrent rows, never her KV (CONTINUUM-ENGINE +// 4.15: a request whose prefix matches her up to a stable boundary prefills only what follows it). +struct llama_persona_checkpoint_blocks { + bool taken = false; + llama_seq_id seq = -1; + llama_pos pos = -1; // her last position held when taken (attention and recurrent agree) + uint64_t rs_layout = 0; + llama_rs_seq_blocks rs; }; const char * llama_persona_move_name(llama_persona_move m); diff --git a/tests/test-persona-blocks.cpp b/tests/test-persona-blocks.cpp index 16dbbfe63539..13bbca9a3954 100644 --- a/tests/test-persona-blocks.cpp +++ b/tests/test-persona-blocks.cpp @@ -377,6 +377,50 @@ int main(int argc, char ** argv) { llama_persona_free(p); } + // 8. what this catches (CONTINUUM-ENGINE 4.15): a mind re-prefilling her whole prompt every act because the engine + // could not go back to a stable boundary (the M5, 2026-11-18: reused 0 of 11k-30k tokens on every act). A + // checkpoint taken at her prompt's end, after she decoded past it, rewinds her so a DIFFERENT continuation at + // that position is byte-identical to decoding it in place; the same checkpoint rewinds twice (it is not consumed); + // a rewind of a checkpoint never taken, or one ahead of what she holds, is refused and changes nothing. + { + auto st = [](int32_t s) { return (llama_persona_move) s; }; + const llama_token other = 9 % f.n_vocab; + // the reference: her prompt, then a token she never decoded in the checkpointed run + llama_memory_clear(llama_get_memory(f.ctx), true); + f.prompt(0, 1); + const std::vector in_place = f.decode_one(0, other, f.n_prompt); + + llama_persona_checkpoint * cp = llama_persona_checkpoint_init(); + require(cp != nullptr, "a checkpoint"); + llama_memory_clear(llama_get_memory(f.ctx), true); + f.prompt(0, 1); + require_move(st(llama_persona_checkpoint_rewind(f.ctx, 0, cp)), llama_persona_move::no_checkpoint, "a rewind of nothing taken"); + require(f.pos_max(0) == f.n_prompt - 1, "a refused rewind leaves her as she was"); + require_move(st(llama_persona_checkpoint_take(f.ctx, 0, cp)), llama_persona_move::done, "checkpoint at her prompt's end"); + require(llama_persona_checkpoint_pos(cp) == f.n_prompt - 1, "it holds through her last prompt position"); + require(f.pos_max(0) == f.n_prompt - 1, "taking a checkpoint leaves her bound and whole"); + for (int round = 0; round < 2; ++round) { + f.decode_one(0, 3 % f.n_vocab, f.n_prompt); // she goes on past the boundary... + f.decode_one(0, 5 % f.n_vocab, f.n_prompt + 1); + require_move(st(llama_persona_checkpoint_rewind(f.ctx, 0, cp)), llama_persona_move::done, "rewind to the boundary"); + require(f.pos_max(0) == f.n_prompt - 1, "what she held past the boundary is cut"); + char label[96]; + snprintf(label, sizeof(label), "a different continuation after rewind %d", round + 1); + require_identical(label, in_place, f.decode_one(0, other, f.n_prompt)); // ...and continues as if she never had + require_move(st(llama_persona_checkpoint_rewind(f.ctx, 0, cp)), llama_persona_move::done, "rewind again before the next round"); + } + // ahead: she holds less than the checkpoint does + llama_memory_clear(llama_get_memory(f.ctx), true); + const int full = f.n_prompt; + f.n_prompt = full / 2; + f.prompt(0, 1); + require_move(st(llama_persona_checkpoint_rewind(f.ctx, 0, cp)), llama_persona_move::checkpoint_ahead, "a rewind never moves forward"); + require(f.pos_max(0) == f.n_prompt - 1, "a refused forward rewind leaves her as she was"); + f.n_prompt = full; + fprintf(stderr, "checkpoint: %zu bytes of recurrent rows (attention KV stays in place)\n", llama_persona_checkpoint_bytes(cp)); + llama_persona_checkpoint_free(cp); + } + fprintf(stderr, "test-persona-blocks: ok (%zu attention layers, %zu recurrent layers)\n", a.kv.k.size(), a.rs.r.size()); return 0; } From 12a98779bbc334e713cc81c1b09f253bdacb707d Mon Sep 17 00:00:00 2001 From: Joel Teply Date: Wed, 18 Nov 2026 18:41:50 -0600 Subject: [PATCH 2/4] llama: a persona checkpoint is bound to her owner and her cells' lineage (Codex on #153) - A rewind checked only slot validity and position, so after a slot was reused the same layout and position could splice A's recurrent rows onto B's KV. Now llama_kv_cells carries a lineage, minted fresh on every reset and swapped with her cells between streams, and the lowest position cut since her last checkpoint (seq_rm, seq_add, seq_div note it). A rewind is refused checkpoint_stale for cells of another lineage or cut at or below its position, and wrong_owner for another persona or epoch (take and rewind now name the owner). - A take on a memory with no persona move returned before clearing the old checkpoint; every refused take now leaves none. - llama_persona_checkpoint_bytes reports allocated capacity, what the governor must count, not the last take's size. - test-persona-blocks case 8 adds: B in A's slot (same layout, same length) refuses A's checkpoint as stale and under B's owner as wrong_owner, B untouched; and A's checkpoint follows her through a save and a load into the other stream, continuing byte-identically. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01LoTjvf5j3Ez13g6k8mRkFo --- include/llama.h | 10 +++++++--- src/llama-context.cpp | 28 +++++++++++++++++----------- src/llama-context.h | 4 ++-- src/llama-kv-cache.cpp | 14 +++++++++++++- src/llama-kv-cache.h | 2 +- src/llama-kv-cells.h | 22 ++++++++++++++++++++++ src/llama-memory-hybrid.cpp | 2 +- src/llama-memory-hybrid.h | 2 +- src/llama-memory-recurrent.cpp | 2 +- src/llama-memory-recurrent.h | 2 +- src/llama-memory.cpp | 1 + src/llama-memory.h | 2 +- src/llama-persona-blocks.h | 6 ++++++ tests/test-persona-blocks.cpp | 34 +++++++++++++++++++++++++++++----- 14 files changed, 103 insertions(+), 28 deletions(-) diff --git a/include/llama.h b/include/llama.h index c602ccef75e9..f053551529e9 100644 --- a/include/llama.h +++ b/include/llama.h @@ -957,12 +957,16 @@ extern "C" { // holds past the checkpoint's position, so the next decode continues from there. Both are two-phase: a refused // rewind leaves her exactly as she was; a refused take leaves no checkpoint (a later rewind refuses no_checkpoint). // A rewind never moves forward (checkpoint_ahead). The caller owns WHEN to take one (a stable prompt boundary). + // A checkpoint is stamped with the persona and epoch it was taken for, and with the lineage of the cells it was + // taken from, which travels with her between streams: a rewind for another owner is wrong_owner, and onto cells + // that are not that lineage, or were cut at or below its position since (a slot reused by another mind, a restart, + // an eviction), is checkpoint_stale. Her rows never reach anyone else's KV. struct llama_persona_checkpoint; LLAMA_API struct llama_persona_checkpoint * llama_persona_checkpoint_init(void); LLAMA_API void llama_persona_checkpoint_free(struct llama_persona_checkpoint * checkpoint); - LLAMA_API int32_t llama_persona_checkpoint_take(struct llama_context * ctx, llama_seq_id seq_id, struct llama_persona_checkpoint * out); - LLAMA_API int32_t llama_persona_checkpoint_rewind(struct llama_context * ctx, llama_seq_id seq_id, const struct llama_persona_checkpoint * in); - // The position a taken checkpoint holds through (-1 when none is taken), and the bytes it holds. + LLAMA_API int32_t llama_persona_checkpoint_take(struct llama_context * ctx, llama_seq_id seq_id, uint64_t persona_id, uint64_t epoch, struct llama_persona_checkpoint * out); + LLAMA_API int32_t llama_persona_checkpoint_rewind(struct llama_context * ctx, llama_seq_id seq_id, uint64_t persona_id, uint64_t epoch, const struct llama_persona_checkpoint * in); + // The position a taken checkpoint holds through (-1 when none is taken), and the bytes its buffers hold allocated. LLAMA_API llama_pos llama_persona_checkpoint_pos(const struct llama_persona_checkpoint * checkpoint); LLAMA_API size_t llama_persona_checkpoint_bytes(const struct llama_persona_checkpoint * checkpoint); diff --git a/src/llama-context.cpp b/src/llama-context.cpp index 562e2f5e2fbc..05b83065cb36 100644 --- a/src/llama-context.cpp +++ b/src/llama-context.cpp @@ -2562,27 +2562,32 @@ ggml_cgraph * llama_context::graph_reserve( return gf; } -llama_persona_move llama_context::persona_checkpoint(llama_seq_id seq_id, llama_persona_checkpoint_blocks & out) { +llama_persona_move llama_context::persona_checkpoint(llama_seq_id seq_id, llama_persona_checkpoint_blocks & out, const llama_persona_identity & who) { + // Her buffers are reused take after take (no copy of the previous checkpoint). Any refused take, an unsupported + // memory included, leaves no checkpoint at all, so a later rewind refuses (no_checkpoint) instead of restoring a + // stale one (Codex on #153). + out.taken = false; if (!memory) { return llama_persona_move::unsupported; } synchronize(); // the raw copies read device memory a queued decode may still be writing - // Her buffers are reused take after take (no copy of the previous checkpoint). A refused take leaves no checkpoint - // at all, so a later rewind refuses (no_checkpoint) instead of restoring a stale one. - out.taken = false; const llama_persona_move checked = memory->persona_check_checkpoint(seq_id, out); if (checked != llama_persona_move::done) { return checked; } memory->persona_commit_checkpoint(seq_id, out); + out.owner = who; out.taken = true; return llama_persona_move::done; } -llama_persona_move llama_context::persona_rewind(llama_seq_id seq_id, const llama_persona_checkpoint_blocks & in) { +llama_persona_move llama_context::persona_rewind(llama_seq_id seq_id, const llama_persona_checkpoint_blocks & in, const llama_persona_identity & who) { if (!memory) { return llama_persona_move::unsupported; } + if (in.taken && (in.owner.persona != who.persona || in.owner.epoch != who.epoch)) { + return llama_persona_move::wrong_owner; // refused before anything is written + } synchronize(); const llama_persona_move checked = memory->persona_check_rewind(seq_id, in); if (checked != llama_persona_move::done) { @@ -4392,12 +4397,12 @@ void llama_persona_checkpoint_free(llama_persona_checkpoint * checkpoint) { delete checkpoint; } -int32_t llama_persona_checkpoint_take(llama_context * ctx, llama_seq_id seq_id, llama_persona_checkpoint * out) { - return (int32_t) ctx->persona_checkpoint(seq_id, out->blocks); +int32_t llama_persona_checkpoint_take(llama_context * ctx, llama_seq_id seq_id, uint64_t persona_id, uint64_t epoch, llama_persona_checkpoint * out) { + return (int32_t) ctx->persona_checkpoint(seq_id, out->blocks, llama_persona_identity { persona_id, epoch }); } -int32_t llama_persona_checkpoint_rewind(llama_context * ctx, llama_seq_id seq_id, const llama_persona_checkpoint * in) { - return (int32_t) ctx->persona_rewind(seq_id, in->blocks); +int32_t llama_persona_checkpoint_rewind(llama_context * ctx, llama_seq_id seq_id, uint64_t persona_id, uint64_t epoch, const llama_persona_checkpoint * in) { + return (int32_t) ctx->persona_rewind(seq_id, in->blocks, llama_persona_identity { persona_id, epoch }); } llama_pos llama_persona_checkpoint_pos(const llama_persona_checkpoint * checkpoint) { @@ -4405,12 +4410,13 @@ llama_pos llama_persona_checkpoint_pos(const llama_persona_checkpoint * checkpoi } size_t llama_persona_checkpoint_bytes(const llama_persona_checkpoint * checkpoint) { + // what its buffers hold allocated (capacity, not the last take's size): what the governor must count size_t bytes = 0; for (const auto & row : checkpoint->blocks.rs.r) { - bytes += row.size(); + bytes += row.capacity(); } for (const auto & row : checkpoint->blocks.rs.s) { - bytes += row.size(); + bytes += row.capacity(); } return bytes; } diff --git a/src/llama-context.h b/src/llama-context.h index 7c5488ffb681..f09b7121fd7e 100644 --- a/src/llama-context.h +++ b/src/llama-context.h @@ -66,8 +66,8 @@ struct llama_context { llama_persona_move persona_load(llama_seq_id seq_id, uint32_t stream, llama_persona_blocks & in, const llama_persona_identity & who); // load her into an empty stream the memory chooses (no_empty_stream when none is) llama_persona_move persona_bind(llama_seq_id seq_id, llama_persona_blocks & in, const llama_persona_identity & who); - llama_persona_move persona_checkpoint(llama_seq_id seq_id, llama_persona_checkpoint_blocks & out); - llama_persona_move persona_rewind(llama_seq_id seq_id, const llama_persona_checkpoint_blocks & in); + llama_persona_move persona_checkpoint(llama_seq_id seq_id, llama_persona_checkpoint_blocks & out, const llama_persona_identity & who); + llama_persona_move persona_rewind(llama_seq_id seq_id, const llama_persona_checkpoint_blocks & in, const llama_persona_identity & who); bool opt_state_write_initial(FILE * optimizer, const char * binding); bool opt_state_write(FILE * optimizer, const char * binding); bool opt_shadow_state_write(const llama_adapter_lora * shadow, FILE * optimizer, const char * binding); diff --git a/src/llama-kv-cache.cpp b/src/llama-kv-cache.cpp index 9d2571e699ad..aa4288bacff9 100644 --- a/src/llama-kv-cache.cpp +++ b/src/llama-kv-cache.cpp @@ -412,6 +412,7 @@ bool llama_kv_cache::seq_rm(llama_seq_id seq_id, llama_pos p0, llama_pos p1) { if (seq_id >= 0) { auto & cells = v_cells[seq_to_stream[seq_id]]; auto & head = v_heads[seq_to_stream[seq_id]]; + cells.note_cut(p0); // a checkpoint at or past p0 no longer holds what it counted on uint32_t new_head = cells.size(); @@ -438,6 +439,7 @@ bool llama_kv_cache::seq_rm(llama_seq_id seq_id, llama_pos p0, llama_pos p1) { auto & head = v_heads[s]; uint32_t new_head = cells.size(); + cells.note_cut(p0); // every sequence in this stream loses what a checkpoint past p0 counted on for (uint32_t i = 0; i < cells.size(); ++i) { if (!cells.pos_in(i, p0, p1)) { @@ -598,6 +600,7 @@ void llama_kv_cache::seq_add(llama_seq_id seq_id, llama_pos p0, llama_pos p1, ll if (shift == 0) { return; } + cells.note_cut(std::max(p0, 0)); // a shift moves what a checkpoint past p0 counted on uint32_t new_head = cells.size(); @@ -648,6 +651,7 @@ void llama_kv_cache::seq_div(llama_seq_id seq_id, llama_pos p0, llama_pos p1, in if (d == 1) { return; } + cells.note_cut(std::max(p0, 0)); // a division moves what a checkpoint past p0 counted on if (p0 < 0) { p0 = 0; @@ -1334,11 +1338,13 @@ llama_persona_move llama_kv_cache::persona_check_checkpoint(llama_seq_id seq_id, return llama_persona_move::invalid_seq; } out.pos = v_cells[seq_to_stream[seq_id]].seq_pos_max(seq_id); + out.lineage = v_cells[seq_to_stream[seq_id]].lineage(); return llama_persona_move::done; } -void llama_kv_cache::persona_commit_checkpoint(llama_seq_id seq_id, llama_persona_checkpoint_blocks & out) const { +void llama_kv_cache::persona_commit_checkpoint(llama_seq_id seq_id, llama_persona_checkpoint_blocks & out) { out.seq = seq_id; + v_cells[seq_to_stream[seq_id]].mark_checkpoint(); // cuts are counted from here on } llama_persona_move llama_kv_cache::persona_check_rewind(llama_seq_id seq_id, const llama_persona_checkpoint_blocks & in) const { @@ -1351,6 +1357,12 @@ llama_persona_move llama_kv_cache::persona_check_rewind(llama_seq_id seq_id, con if (in.pos > v_cells[seq_to_stream[seq_id]].seq_pos_max(seq_id)) { return llama_persona_move::checkpoint_ahead; } + // the cells must be the ones it was taken from, uncut at or below its position since (Codex on #153: a slot + // reused by another mind must never receive these rows) + const auto & cells = v_cells[seq_to_stream[seq_id]]; + if (cells.lineage() != in.lineage || cells.cut_low() <= in.pos) { + return llama_persona_move::checkpoint_stale; + } return llama_persona_move::done; } diff --git a/src/llama-kv-cache.h b/src/llama-kv-cache.h index 71998706311a..c9346b8b67dd 100644 --- a/src/llama-kv-cache.h +++ b/src/llama-kv-cache.h @@ -187,7 +187,7 @@ class llama_kv_cache : public llama_memory_i { void persona_commit_save(llama_seq_id seq_id, llama_persona_blocks & out) override; void persona_commit_load(llama_seq_id seq_id, llama_persona_slot & slot, llama_persona_blocks & in) override; llama_persona_move persona_check_checkpoint(llama_seq_id seq_id, llama_persona_checkpoint_blocks & out) const override; - void persona_commit_checkpoint(llama_seq_id seq_id, llama_persona_checkpoint_blocks & out) const override; + void persona_commit_checkpoint(llama_seq_id seq_id, llama_persona_checkpoint_blocks & out) override; llama_persona_move persona_check_rewind(llama_seq_id seq_id, const llama_persona_checkpoint_blocks & in) const override; void persona_commit_rewind(llama_seq_id seq_id, const llama_persona_checkpoint_blocks & in) override; diff --git a/src/llama-kv-cells.h b/src/llama-kv-cells.h index 2196dbfb38e3..5299975b4de0 100644 --- a/src/llama-kv-cells.h +++ b/src/llama-kv-cells.h @@ -4,7 +4,10 @@ #include "llama-cparams.h" #include "llama-kv-cell-index.h" +#include +#include #include +#include #include #include #include @@ -45,9 +48,21 @@ class llama_kv_cells { seq_pos[s].swap(other.seq_pos[s]); } std::swap(stale, other.stale); + std::swap(lineage_id, other.lineage_id); + std::swap(lowest_cut, other.lowest_cut); } + // Continuum fork (CONTINUUM-ENGINE 4.15): whose history these cells hold, and the lowest position cut from it since + // her last checkpoint. A lineage is minted fresh whenever the cells are reset (or made), and travels with them on a + // swap, so a checkpoint follows her between streams and is refused for anyone else's cells in the same slot. + uint64_t lineage() const { return lineage_id; } + llama_pos cut_low() const { return lowest_cut; } + void note_cut(llama_pos p0) { lowest_cut = std::min(lowest_cut, p0); } + void mark_checkpoint() { lowest_cut = std::numeric_limits::max(); } + void reset() { + lineage_id = next_lineage(); + lowest_cut = std::numeric_limits::max(); for (uint32_t i = 0; i < pos.size(); ++i) { pos[i] = -1; ext[i].reset(); @@ -519,6 +534,13 @@ class llama_kv_cells { } private: + static uint64_t next_lineage() { + static std::atomic next { 1 }; + return next.fetch_add(1, std::memory_order_relaxed); + } + uint64_t lineage_id = next_lineage(); + llama_pos lowest_cut = std::numeric_limits::max(); + bool has_shift = false; // the occupied cells (i.e. pos[i] != -1, allowed to not have any seq_id): a bitmap with its count and extrema diff --git a/src/llama-memory-hybrid.cpp b/src/llama-memory-hybrid.cpp index 9b49502d9795..821de67314cd 100644 --- a/src/llama-memory-hybrid.cpp +++ b/src/llama-memory-hybrid.cpp @@ -245,7 +245,7 @@ llama_persona_move llama_memory_hybrid::persona_check_checkpoint(llama_seq_id se return attn != llama_persona_move::done ? attn : mem_recr->persona_check_checkpoint(seq_id, out); } -void llama_memory_hybrid::persona_commit_checkpoint(llama_seq_id seq_id, llama_persona_checkpoint_blocks & out) const { +void llama_memory_hybrid::persona_commit_checkpoint(llama_seq_id seq_id, llama_persona_checkpoint_blocks & out) { mem_attn->persona_commit_checkpoint(seq_id, out); mem_recr->persona_commit_checkpoint(seq_id, out); } diff --git a/src/llama-memory-hybrid.h b/src/llama-memory-hybrid.h index ab025fb4696a..747bb27e4989 100644 --- a/src/llama-memory-hybrid.h +++ b/src/llama-memory-hybrid.h @@ -96,7 +96,7 @@ class llama_memory_hybrid : public llama_memory_i { void persona_commit_save(llama_seq_id seq_id, llama_persona_blocks & out) override; void persona_commit_load(llama_seq_id seq_id, llama_persona_slot & slot, llama_persona_blocks & in) override; llama_persona_move persona_check_checkpoint(llama_seq_id seq_id, llama_persona_checkpoint_blocks & out) const override; - void persona_commit_checkpoint(llama_seq_id seq_id, llama_persona_checkpoint_blocks & out) const override; + void persona_commit_checkpoint(llama_seq_id seq_id, llama_persona_checkpoint_blocks & out) override; llama_persona_move persona_check_rewind(llama_seq_id seq_id, const llama_persona_checkpoint_blocks & in) const override; void persona_commit_rewind(llama_seq_id seq_id, const llama_persona_checkpoint_blocks & in) override; diff --git a/src/llama-memory-recurrent.cpp b/src/llama-memory-recurrent.cpp index 595e7a864358..a8e1fd39948f 100644 --- a/src/llama-memory-recurrent.cpp +++ b/src/llama-memory-recurrent.cpp @@ -573,7 +573,7 @@ llama_persona_move llama_memory_recurrent::persona_check_checkpoint(llama_seq_id return llama_persona_move::done; } -void llama_memory_recurrent::persona_commit_checkpoint(llama_seq_id seq_id, llama_persona_checkpoint_blocks & out) const { +void llama_memory_recurrent::persona_commit_checkpoint(llama_seq_id seq_id, llama_persona_checkpoint_blocks & out) { auto & rs = out.rs; const int32_t tail = cells[seq_id].tail; rs.has_state = tail >= 0; diff --git a/src/llama-memory-recurrent.h b/src/llama-memory-recurrent.h index e445d583c46c..83aeb80b4465 100644 --- a/src/llama-memory-recurrent.h +++ b/src/llama-memory-recurrent.h @@ -96,7 +96,7 @@ class llama_memory_recurrent : public llama_memory_i { void persona_commit_save(llama_seq_id seq_id, llama_persona_blocks & out) override; void persona_commit_load(llama_seq_id seq_id, llama_persona_slot & slot, llama_persona_blocks & in) override; llama_persona_move persona_check_checkpoint(llama_seq_id seq_id, llama_persona_checkpoint_blocks & out) const override; - void persona_commit_checkpoint(llama_seq_id seq_id, llama_persona_checkpoint_blocks & out) const override; + void persona_commit_checkpoint(llama_seq_id seq_id, llama_persona_checkpoint_blocks & out) override; llama_persona_move persona_check_rewind(llama_seq_id seq_id, const llama_persona_checkpoint_blocks & in) const override; void persona_commit_rewind(llama_seq_id seq_id, const llama_persona_checkpoint_blocks & in) override; diff --git a/src/llama-memory.cpp b/src/llama-memory.cpp index 00f81a2bb253..5ccaab986de7 100644 --- a/src/llama-memory.cpp +++ b/src/llama-memory.cpp @@ -78,6 +78,7 @@ const char * llama_persona_move_name(llama_persona_move m) { case llama_persona_move::no_empty_stream: return "no_empty_stream"; case llama_persona_move::no_checkpoint: return "no_checkpoint"; case llama_persona_move::checkpoint_ahead: return "checkpoint_ahead"; + case llama_persona_move::checkpoint_stale: return "checkpoint_stale"; } return "unknown"; } diff --git a/src/llama-memory.h b/src/llama-memory.h index a887052e7aa2..2d484e1296f9 100644 --- a/src/llama-memory.h +++ b/src/llama-memory.h @@ -183,7 +183,7 @@ struct llama_memory_i { (void) seq_id; (void) out; return llama_persona_move::unsupported; } - virtual void persona_commit_checkpoint(llama_seq_id seq_id, llama_persona_checkpoint_blocks & out) const { + virtual void persona_commit_checkpoint(llama_seq_id seq_id, llama_persona_checkpoint_blocks & out) { (void) seq_id; (void) out; } virtual llama_persona_move persona_check_rewind(llama_seq_id seq_id, const llama_persona_checkpoint_blocks & in) const { diff --git a/src/llama-persona-blocks.h b/src/llama-persona-blocks.h index 5e6eea976656..dbfd609edf4b 100644 --- a/src/llama-persona-blocks.h +++ b/src/llama-persona-blocks.h @@ -84,6 +84,7 @@ enum class llama_persona_move { no_empty_stream, // a bind found no empty stream to place her in no_checkpoint, // a rewind was asked of a checkpoint never taken checkpoint_ahead, // a rewind was asked to a position past what she holds: a rewind never moves forward + checkpoint_stale, // her cells are not the lineage the checkpoint was taken from, or were cut at or below it since }; // A copy of a bound persona at one of her positions, taken WITHOUT moving her: her recurrent rows (every snapshot @@ -94,6 +95,11 @@ struct llama_persona_checkpoint_blocks { bool taken = false; llama_seq_id seq = -1; llama_pos pos = -1; // her last position held when taken (attention and recurrent agree) + // whose it is (the persona and epoch it was taken for) and the lineage of the cells it was taken from: a rewind is + // refused for another owner, or for cells that are not that lineage or were cut at or below `pos` since (a slot + // reused by another mind, a restart, an eviction), so her rows are never spliced onto someone else's KV + llama_persona_identity owner; + uint64_t lineage = 0; uint64_t rs_layout = 0; llama_rs_seq_blocks rs; }; diff --git a/tests/test-persona-blocks.cpp b/tests/test-persona-blocks.cpp index 13bbca9a3954..c2f4f53b2ba4 100644 --- a/tests/test-persona-blocks.cpp +++ b/tests/test-persona-blocks.cpp @@ -394,27 +394,51 @@ int main(int argc, char ** argv) { require(cp != nullptr, "a checkpoint"); llama_memory_clear(llama_get_memory(f.ctx), true); f.prompt(0, 1); - require_move(st(llama_persona_checkpoint_rewind(f.ctx, 0, cp)), llama_persona_move::no_checkpoint, "a rewind of nothing taken"); + require_move(st(llama_persona_checkpoint_rewind(f.ctx, 0, 0xA, 1, cp)), llama_persona_move::no_checkpoint, "a rewind of nothing taken"); require(f.pos_max(0) == f.n_prompt - 1, "a refused rewind leaves her as she was"); - require_move(st(llama_persona_checkpoint_take(f.ctx, 0, cp)), llama_persona_move::done, "checkpoint at her prompt's end"); + require_move(st(llama_persona_checkpoint_take(f.ctx, 0, 0xA, 1, cp)), llama_persona_move::done, "checkpoint at her prompt's end"); require(llama_persona_checkpoint_pos(cp) == f.n_prompt - 1, "it holds through her last prompt position"); require(f.pos_max(0) == f.n_prompt - 1, "taking a checkpoint leaves her bound and whole"); for (int round = 0; round < 2; ++round) { f.decode_one(0, 3 % f.n_vocab, f.n_prompt); // she goes on past the boundary... f.decode_one(0, 5 % f.n_vocab, f.n_prompt + 1); - require_move(st(llama_persona_checkpoint_rewind(f.ctx, 0, cp)), llama_persona_move::done, "rewind to the boundary"); + require_move(st(llama_persona_checkpoint_rewind(f.ctx, 0, 0xA, 1, cp)), llama_persona_move::done, "rewind to the boundary"); require(f.pos_max(0) == f.n_prompt - 1, "what she held past the boundary is cut"); char label[96]; snprintf(label, sizeof(label), "a different continuation after rewind %d", round + 1); require_identical(label, in_place, f.decode_one(0, other, f.n_prompt)); // ...and continues as if she never had - require_move(st(llama_persona_checkpoint_rewind(f.ctx, 0, cp)), llama_persona_move::done, "rewind again before the next round"); + require_move(st(llama_persona_checkpoint_rewind(f.ctx, 0, 0xA, 1, cp)), llama_persona_move::done, "rewind again before the next round"); } + // what this catches (Codex on #153): A's rows spliced onto B's KV when B reuses A's slot at the same layout and + // position. B in the slot A was checkpointed in: refused as stale under A's owner, as wrong owner under B's, + // and B continues exactly as in place. + const auto ref_b = f.reference(0, 2); + llama_memory_clear(llama_get_memory(f.ctx), true); + f.prompt(0, 1); + require_move(st(llama_persona_checkpoint_take(f.ctx, 0, 0xA, 1, cp)), llama_persona_move::done, "A's checkpoint"); + llama_memory_clear(llama_get_memory(f.ctx), true); + f.prompt(0, 2); // B, same slot, same length + require_move(st(llama_persona_checkpoint_rewind(f.ctx, 0, 0xA, 1, cp)), llama_persona_move::checkpoint_stale, "A's rows never reach B's cells"); + require_move(st(llama_persona_checkpoint_rewind(f.ctx, 0, 0xB, 1, cp)), llama_persona_move::wrong_owner, "nor under B's owner"); + f.require_continues("B untouched by the refused rewinds", 0, ref_b); + + // what this catches: a checkpoint lost when she moves between streams (four minds rotate through two). Taken + // on her stream, she is saved and bound into the other one, and the rewind there continues byte-identically. + llama_memory_clear(llama_get_memory(f.ctx), true); + f.prompt(0, 1); + require_move(st(llama_persona_checkpoint_take(f.ctx, 0, 0xA, 1, cp)), llama_persona_move::done, "A's checkpoint before her move"); + f.decode_one(0, 3 % f.n_vocab, f.n_prompt); + require_move(ctx.persona_save(0, a, A), llama_persona_move::done, "A out"); + require_move(ctx.persona_load(0, 1 - llama_get_memory(f.ctx)->seq_stream(0), a, A), llama_persona_move::done, "A into the other stream"); + require_move(st(llama_persona_checkpoint_rewind(f.ctx, 0, 0xA, 1, cp)), llama_persona_move::done, "her checkpoint follows her"); + require_identical("a different continuation after her move and rewind", in_place, f.decode_one(0, other, f.n_prompt)); + // ahead: she holds less than the checkpoint does llama_memory_clear(llama_get_memory(f.ctx), true); const int full = f.n_prompt; f.n_prompt = full / 2; f.prompt(0, 1); - require_move(st(llama_persona_checkpoint_rewind(f.ctx, 0, cp)), llama_persona_move::checkpoint_ahead, "a rewind never moves forward"); + require_move(st(llama_persona_checkpoint_rewind(f.ctx, 0, 0xA, 1, cp)), llama_persona_move::checkpoint_ahead, "a rewind never moves forward"); require(f.pos_max(0) == f.n_prompt - 1, "a refused forward rewind leaves her as she was"); f.n_prompt = full; fprintf(stderr, "checkpoint: %zu bytes of recurrent rows (attention KV stays in place)\n", llama_persona_checkpoint_bytes(cp)); From a58c3b4cfa48102ead6f7568bc78534639b1506a Mon Sep 17 00:00:00 2001 From: Joel Teply Date: Wed, 18 Nov 2026 18:48:24 -0600 Subject: [PATCH 3/4] llama: only the newest checkpoint on a lineage rewinds (a checkpoint serial on her cells; Codex on #153 at 12a98779b) A take reset the lowest-cut receipt, so an older checkpoint whose positions were cut and refilled to the same length kept rewinding. The cells now carry a checkpoint serial (swapped with them); each take bumps it and a rewind requires it. test-persona-blocks case 8: take A, cut below it and refill a different suffix, take B; A is refused checkpoint_stale and B still rewinds. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01LoTjvf5j3Ez13g6k8mRkFo --- src/llama-kv-cache.cpp | 4 ++-- src/llama-kv-cells.h | 7 ++++++- src/llama-persona-blocks.h | 1 + tests/test-persona-blocks.cpp | 20 ++++++++++++++++++++ 4 files changed, 29 insertions(+), 3 deletions(-) diff --git a/src/llama-kv-cache.cpp b/src/llama-kv-cache.cpp index aa4288bacff9..d2d69e4b0d8a 100644 --- a/src/llama-kv-cache.cpp +++ b/src/llama-kv-cache.cpp @@ -1344,7 +1344,7 @@ llama_persona_move llama_kv_cache::persona_check_checkpoint(llama_seq_id seq_id, void llama_kv_cache::persona_commit_checkpoint(llama_seq_id seq_id, llama_persona_checkpoint_blocks & out) { out.seq = seq_id; - v_cells[seq_to_stream[seq_id]].mark_checkpoint(); // cuts are counted from here on + out.serial = v_cells[seq_to_stream[seq_id]].mark_checkpoint(); // cuts are counted from here on, for this checkpoint only } llama_persona_move llama_kv_cache::persona_check_rewind(llama_seq_id seq_id, const llama_persona_checkpoint_blocks & in) const { @@ -1360,7 +1360,7 @@ llama_persona_move llama_kv_cache::persona_check_rewind(llama_seq_id seq_id, con // the cells must be the ones it was taken from, uncut at or below its position since (Codex on #153: a slot // reused by another mind must never receive these rows) const auto & cells = v_cells[seq_to_stream[seq_id]]; - if (cells.lineage() != in.lineage || cells.cut_low() <= in.pos) { + if (cells.lineage() != in.lineage || cells.checkpoint() != in.serial || cells.cut_low() <= in.pos) { return llama_persona_move::checkpoint_stale; } return llama_persona_move::done; diff --git a/src/llama-kv-cells.h b/src/llama-kv-cells.h index 5299975b4de0..19f2e43ad657 100644 --- a/src/llama-kv-cells.h +++ b/src/llama-kv-cells.h @@ -50,6 +50,7 @@ class llama_kv_cells { std::swap(stale, other.stale); std::swap(lineage_id, other.lineage_id); std::swap(lowest_cut, other.lowest_cut); + std::swap(checkpoint_serial, other.checkpoint_serial); } // Continuum fork (CONTINUUM-ENGINE 4.15): whose history these cells hold, and the lowest position cut from it since @@ -58,7 +59,10 @@ class llama_kv_cells { uint64_t lineage() const { return lineage_id; } llama_pos cut_low() const { return lowest_cut; } void note_cut(llama_pos p0) { lowest_cut = std::min(lowest_cut, p0); } - void mark_checkpoint() { lowest_cut = std::numeric_limits::max(); } + // Only the newest checkpoint on a lineage rewinds: each take bumps the serial and starts counting cuts afresh, so + // an older one (whose cut receipt that reset erased) is refused by serial (Codex on #153). + uint64_t checkpoint() const { return checkpoint_serial; } + uint64_t mark_checkpoint() { lowest_cut = std::numeric_limits::max(); return ++checkpoint_serial; } void reset() { lineage_id = next_lineage(); @@ -540,6 +544,7 @@ class llama_kv_cells { } uint64_t lineage_id = next_lineage(); llama_pos lowest_cut = std::numeric_limits::max(); + uint64_t checkpoint_serial = 0; bool has_shift = false; diff --git a/src/llama-persona-blocks.h b/src/llama-persona-blocks.h index dbfd609edf4b..c80e8ca05900 100644 --- a/src/llama-persona-blocks.h +++ b/src/llama-persona-blocks.h @@ -100,6 +100,7 @@ struct llama_persona_checkpoint_blocks { // reused by another mind, a restart, an eviction), so her rows are never spliced onto someone else's KV llama_persona_identity owner; uint64_t lineage = 0; + uint64_t serial = 0; // which checkpoint of that lineage: only the newest rewinds uint64_t rs_layout = 0; llama_rs_seq_blocks rs; }; diff --git a/tests/test-persona-blocks.cpp b/tests/test-persona-blocks.cpp index c2f4f53b2ba4..a59a46a8a191 100644 --- a/tests/test-persona-blocks.cpp +++ b/tests/test-persona-blocks.cpp @@ -433,6 +433,26 @@ int main(int argc, char ** argv) { require_move(st(llama_persona_checkpoint_rewind(f.ctx, 0, 0xA, 1, cp)), llama_persona_move::done, "her checkpoint follows her"); require_identical("a different continuation after her move and rewind", in_place, f.decode_one(0, other, f.n_prompt)); + // what this catches (Codex on #153 at 12a98779b): a newer take erasing the cut receipt an older checkpoint + // needed. Take A, cut below it and refill a different suffix to the same length, take B: A is stale (by its + // serial), B still rewinds. + { + llama_persona_checkpoint * newer = llama_persona_checkpoint_init(); + require(newer != nullptr, "a second checkpoint"); + llama_memory_clear(llama_get_memory(f.ctx), true); + f.prompt(0, 1); + require_move(st(llama_persona_checkpoint_take(f.ctx, 0, 0xA, 1, cp)), llama_persona_move::done, "checkpoint A"); + const llama_pos half = f.n_prompt / 2; + require(llama_memory_seq_rm(llama_get_memory(f.ctx), 0, half, -1), "cut below A"); + for (llama_pos p = half; p < f.n_prompt; ++p) { + f.decode_one(0, 11 % f.n_vocab, p); // a different suffix, to the same length + } + require_move(st(llama_persona_checkpoint_take(f.ctx, 0, 0xA, 1, newer)), llama_persona_move::done, "checkpoint B"); + require_move(st(llama_persona_checkpoint_rewind(f.ctx, 0, 0xA, 1, cp)), llama_persona_move::checkpoint_stale, "A no longer matches what she holds"); + require_move(st(llama_persona_checkpoint_rewind(f.ctx, 0, 0xA, 1, newer)), llama_persona_move::done, "B, the newest, still rewinds"); + llama_persona_checkpoint_free(newer); + } + // ahead: she holds less than the checkpoint does llama_memory_clear(llama_get_memory(f.ctx), true); const int full = f.n_prompt; From 1fef0120773d2e558679ed553626f7e3799f2077 Mon Sep 17 00:00:00 2001 From: Joel Teply Date: Thu, 19 Nov 2026 02:48:08 -0600 Subject: [PATCH 4/4] llama: a checkpoint of standalone recurrent memory is refused: only a hybrid's attention lineage guards a rewind Codex on CambrianTech/llama.cpp#153: on pure recurrent memory persona_check_rewind had no lineage, newest-take or lowest-cut check, so after slot reuse an unshared tail at a sufficient position could pass where the KV path would refuse. Recurrent memory has no lineage to check, so it no longer overrides the checkpoint verbs: standalone, it inherits the base refusal (unsupported). Its row copies become persona_check_checkpoint_rows and siblings, called only by llama_memory_hybrid after its attention half has checked lineage, serial and lowest cut. Built and tested inside persona-files (test-persona-blocks on the hybrid test model and synthetic qwen35moe, both ok, 2026-11-19 08:47Z). test-persona-blocks does not run on pure recurrent memory: on synthetic mamba it fails at an earlier attention-only stream case ('load over b's stream'), so the standalone refusal holds by construction (no override), not by that test. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01LoTjvf5j3Ez13g6k8mRkFo --- src/llama-memory-hybrid.cpp | 8 ++++---- src/llama-memory-recurrent.cpp | 8 ++++---- src/llama-memory-recurrent.h | 12 ++++++++---- 3 files changed, 16 insertions(+), 12 deletions(-) diff --git a/src/llama-memory-hybrid.cpp b/src/llama-memory-hybrid.cpp index 821de67314cd..87d8a83f4c76 100644 --- a/src/llama-memory-hybrid.cpp +++ b/src/llama-memory-hybrid.cpp @@ -242,12 +242,12 @@ void llama_memory_hybrid::persona_commit_load(llama_seq_id seq_id, llama_persona // the same token. llama_persona_move llama_memory_hybrid::persona_check_checkpoint(llama_seq_id seq_id, llama_persona_checkpoint_blocks & out) const { const llama_persona_move attn = mem_attn->persona_check_checkpoint(seq_id, out); - return attn != llama_persona_move::done ? attn : mem_recr->persona_check_checkpoint(seq_id, out); + return attn != llama_persona_move::done ? attn : mem_recr->persona_check_checkpoint_rows(seq_id, out); } void llama_memory_hybrid::persona_commit_checkpoint(llama_seq_id seq_id, llama_persona_checkpoint_blocks & out) { mem_attn->persona_commit_checkpoint(seq_id, out); - mem_recr->persona_commit_checkpoint(seq_id, out); + mem_recr->persona_commit_checkpoint_rows(seq_id, out); } llama_persona_move llama_memory_hybrid::persona_check_rewind(llama_seq_id seq_id, const llama_persona_checkpoint_blocks & in) const { @@ -255,12 +255,12 @@ llama_persona_move llama_memory_hybrid::persona_check_rewind(llama_seq_id seq_id return llama_persona_move::layout_mismatch; // the halves were not at one token when it was taken } const llama_persona_move attn = mem_attn->persona_check_rewind(seq_id, in); - return attn != llama_persona_move::done ? attn : mem_recr->persona_check_rewind(seq_id, in); + return attn != llama_persona_move::done ? attn : mem_recr->persona_check_rewind_rows(seq_id, in); } void llama_memory_hybrid::persona_commit_rewind(llama_seq_id seq_id, const llama_persona_checkpoint_blocks & in) { mem_attn->persona_commit_rewind(seq_id, in); - mem_recr->persona_commit_rewind(seq_id, in); + mem_recr->persona_commit_rewind_rows(seq_id, in); } llama_kv_cache * llama_memory_hybrid::get_mem_attn() const { diff --git a/src/llama-memory-recurrent.cpp b/src/llama-memory-recurrent.cpp index a8e1fd39948f..07d8e3a63c00 100644 --- a/src/llama-memory-recurrent.cpp +++ b/src/llama-memory-recurrent.cpp @@ -553,7 +553,7 @@ void llama_memory_recurrent::persona_commit_load(llama_seq_id seq_id, llama_pers // A checkpoint copies her recurrent rows (every snapshot group) out of her tail cell and leaves her bound: unlike a // save, her cell, position and rollback index stay as they are. -llama_persona_move llama_memory_recurrent::persona_check_checkpoint(llama_seq_id seq_id, llama_persona_checkpoint_blocks & out) const { +llama_persona_move llama_memory_recurrent::persona_check_checkpoint_rows(llama_seq_id seq_id, llama_persona_checkpoint_blocks & out) const { if (seq_id < 0 || (uint32_t) seq_id >= size || (uint32_t) seq_id >= n_seq_max) { return llama_persona_move::invalid_seq; } @@ -573,7 +573,7 @@ llama_persona_move llama_memory_recurrent::persona_check_checkpoint(llama_seq_id return llama_persona_move::done; } -void llama_memory_recurrent::persona_commit_checkpoint(llama_seq_id seq_id, llama_persona_checkpoint_blocks & out) { +void llama_memory_recurrent::persona_commit_checkpoint_rows(llama_seq_id seq_id, llama_persona_checkpoint_blocks & out) { auto & rs = out.rs; const int32_t tail = cells[seq_id].tail; rs.has_state = tail >= 0; @@ -599,7 +599,7 @@ void llama_memory_recurrent::persona_commit_checkpoint(llama_seq_id seq_id, llam rs.rs_idx = rs_idx[seq_id]; } -llama_persona_move llama_memory_recurrent::persona_check_rewind(llama_seq_id seq_id, const llama_persona_checkpoint_blocks & in) const { +llama_persona_move llama_memory_recurrent::persona_check_rewind_rows(llama_seq_id seq_id, const llama_persona_checkpoint_blocks & in) const { if (seq_id < 0 || (uint32_t) seq_id >= size || (uint32_t) seq_id >= n_seq_max) { return llama_persona_move::invalid_seq; } @@ -641,7 +641,7 @@ llama_persona_move llama_memory_recurrent::persona_check_rewind(llama_seq_id seq return llama_persona_move::done; } -void llama_memory_recurrent::persona_commit_rewind(llama_seq_id seq_id, const llama_persona_checkpoint_blocks & in) { +void llama_memory_recurrent::persona_commit_rewind_rows(llama_seq_id seq_id, const llama_persona_checkpoint_blocks & in) { const auto & rs = in.rs; if (!rs.has_state) { return; diff --git a/src/llama-memory-recurrent.h b/src/llama-memory-recurrent.h index 83aeb80b4465..b2511657508c 100644 --- a/src/llama-memory-recurrent.h +++ b/src/llama-memory-recurrent.h @@ -95,10 +95,14 @@ class llama_memory_recurrent : public llama_memory_i { llama_persona_slot & slot) const override; void persona_commit_save(llama_seq_id seq_id, llama_persona_blocks & out) override; void persona_commit_load(llama_seq_id seq_id, llama_persona_slot & slot, llama_persona_blocks & in) override; - llama_persona_move persona_check_checkpoint(llama_seq_id seq_id, llama_persona_checkpoint_blocks & out) const override; - void persona_commit_checkpoint(llama_seq_id seq_id, llama_persona_checkpoint_blocks & out) override; - llama_persona_move persona_check_rewind(llama_seq_id seq_id, const llama_persona_checkpoint_blocks & in) const override; - void persona_commit_rewind(llama_seq_id seq_id, const llama_persona_checkpoint_blocks & in) override; + // Her recurrent rows for a checkpoint and a rewind, used only by the hybrid memory, whose attention half checks the + // lineage, the newest-take serial and the lowest cut before these run. Standalone recurrent memory has no lineage to + // check, so it does not override the checkpoint verbs: a checkpoint of it is refused as unsupported, never rewound + // onto a reused slot (Codex on CambrianTech/llama.cpp#153). + llama_persona_move persona_check_checkpoint_rows(llama_seq_id seq_id, llama_persona_checkpoint_blocks & out) const; + void persona_commit_checkpoint_rows(llama_seq_id seq_id, llama_persona_checkpoint_blocks & out); + llama_persona_move persona_check_rewind_rows(llama_seq_id seq_id, const llama_persona_checkpoint_blocks & in) const; + void persona_commit_rewind_rows(llama_seq_id seq_id, const llama_persona_checkpoint_blocks & in); // an undo of a draft is pending on this sequence: its next read takes a snapshot group, not the // live state (group 0) a training graph reads and writes (llm_graph_context::rs_keep)