Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions include/llama.h
Original file line number Diff line number Diff line change
Expand Up @@ -1071,6 +1071,14 @@ extern "C" {
// 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);
// Her checkpoint across a restart, beside her record of the same stop's generation. A write takes only a checkpoint
// still valid against the parked blocks it is written with (her owner, her cells' lineage, uncut at or below its
// position since it was taken); a read fills `out` for blocks already read back from her record of that generation,
// checked against them (owner, epoch, sequence, recurrent layout, every row length from their geometry, a position
// they hold), and binds it to their restored cells, so it rewinds exactly as one taken before the stop. A refused
// read leaves `out` holding nothing taken. 0 when done, else a status for llama_persona_status_name.
LLAMA_API int32_t llama_persona_checkpoint_write_file(const struct llama_persona * persona, const struct llama_persona_checkpoint * checkpoint, const char * path, uint64_t generation);
LLAMA_API int32_t llama_persona_checkpoint_read_file(struct llama_persona * persona, const char * path, uint64_t generation, struct llama_persona_checkpoint * out);

// 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
Expand Down
4 changes: 0 additions & 4 deletions src/llama-context-persona.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,10 +182,6 @@ 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();
}
Expand Down
142 changes: 142 additions & 0 deletions src/llama-persona-file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <new>
#ifdef _WIN32
# ifndef WIN32_LEAN_AND_MEAN
# define WIN32_LEAN_AND_MEAN
Expand Down Expand Up @@ -377,3 +378,144 @@ int32_t llama_persona_read_file(llama_persona * persona, const char * path, uint
b.owner_epoch = epoch;
return (int32_t) llama_persona_move::done;
}

// Her checkpoint across a restart (CONTINUUM-ENGINE 4.15 and 4.16): the recurrent rows a checkpoint holds and where she
// was, beside her record of the same stop. Without it a hybrid mind restored with every token she held could not rewind
// to her view's end, so the first turn after a restart re-prefilled her whole prompt (the M5, 2026-11-27: Aiko shared
// 214,911 of 246,764 tokens with what she held and prefilled 1,019 s). Its validity before the stop is a receipt in her
// cells (lineage and an uncut mark); a restart gives her new cells, so the read stamps it with theirs: a checkpoint is
// written only while that receipt holds against the blocks it is written with, and read only into blocks restored
// from the record of the same generation, holding its position.
namespace {

constexpr uint32_t CHECKPOINT_MAGIC = 0x314B5043; // "CPK1"
constexpr uint32_t CHECKPOINT_VERSION = 1;

} // namespace

static int32_t checkpoint_write_file(const llama_persona * persona, const llama_persona_checkpoint * checkpoint, const char * path, uint64_t generation) {
const auto & b = persona->blocks;
const auto & c = checkpoint->blocks;
if (!c.taken) {
return (int32_t) llama_persona_move::no_checkpoint;
}
if (!b.owned || c.owner.persona != b.owner || c.owner.epoch != b.owner_epoch) {
return (int32_t) llama_persona_move::wrong_owner;
}
if (c.rs_layout != b.rs_layout) {
return (int32_t) llama_persona_move::layout_mismatch;
}
// still hers as taken: the cells it was taken from, uncut at or below its position since, holding it
if (b.kv.cells.lineage() != c.lineage || !b.kv.cells.holds_checkpoint(c.mark)) {
return (int32_t) llama_persona_move::checkpoint_stale;
}
if (b.seq < 0 || c.pos > b.kv.cells.seq_pos_max(b.seq)) {
return (int32_t) llama_persona_move::checkpoint_ahead;
}
const std::string tmp = std::string(path) + ".tmp";
std::FILE * f = std::fopen(tmp.c_str(), "wb");
if (f == nullptr) {
return (int32_t) llama_persona_move::io_error;
}
writer w { f };
w.put(CHECKPOINT_MAGIC);
w.put(CHECKPOINT_VERSION);
w.put<uint64_t>(b.owner);
w.put<uint64_t>(b.owner_epoch);
w.put<uint64_t>(b.rs_layout);
w.put<uint64_t>(generation);
w.put<llama_pos>(c.pos);
w.put<uint8_t>(c.rs.has_state ? 1 : 0);
w.put<llama_pos>(c.rs.pos);
w.put<uint32_t>(c.rs.rs_idx);
w.rows(c.rs.r);
w.rows(c.rs.s);
const bool flushed = w.ok && std::fflush(f) == 0;
const bool closed = std::fclose(f) == 0;
if (!flushed || !closed || !replace_file(tmp, path)) {
std::remove(tmp.c_str());
return (int32_t) llama_persona_move::io_error;
}
return (int32_t) llama_persona_move::done;
}

static int32_t checkpoint_read_file(llama_persona * persona, const char * path, uint64_t generation, llama_persona_checkpoint * out) {
auto & b = persona->blocks;
auto & c = out->blocks;
c.taken = false; // a refused read leaves nothing taken, never a stale checkpoint
if (!b.owned) {
return (int32_t) llama_persona_move::file_mismatch; // her record was not read back: nothing to bind it to
}
std::FILE * f = std::fopen(path, "rb");
if (f == nullptr) {
return (int32_t) llama_persona_move::io_error;
}
closes guard { f };
reader r { f };
const auto mismatch = (int32_t) llama_persona_move::file_mismatch;
const uint32_t magic = r.get<uint32_t>();
const uint32_t version = r.get<uint32_t>();
const uint64_t owner = r.get<uint64_t>();
const uint64_t epoch = r.get<uint64_t>();
const uint64_t layout = r.get<uint64_t>();
const uint64_t written = r.get<uint64_t>();
const llama_pos pos = r.get<llama_pos>();
const bool has_state = r.get<uint8_t>() != 0;
const llama_pos rs_pos = r.get<llama_pos>();
const uint32_t rs_idx = r.get<uint32_t>();
if (!r.ok) {
return (int32_t) llama_persona_move::io_error;
}
// hers, of this stop, for this memory, at a position her restored cells hold
if (magic != CHECKPOINT_MAGIC || version != CHECKPOINT_VERSION || owner != b.owner || epoch != b.owner_epoch ||
layout != b.rs_layout || written != generation || pos < 0 || b.seq < 0 || pos > b.kv.cells.seq_pos_max(b.seq)) {
return mismatch;
}
// every row length from this memory's geometry (persona_prepare), never one the file claims
const auto & g = b.geometry;
const std::vector<size_t> r_bytes = has_state ? g.r_bytes : std::vector<size_t>(g.r_bytes.size(), 0);
const std::vector<size_t> s_bytes = has_state ? g.s_bytes : std::vector<size_t>(g.s_bytes.size(), 0);
std::vector<std::vector<uint8_t>> rs_r, rs_s;
if (!r.rows(rs_r, r_bytes) || !r.rows(rs_s, s_bytes)) {
return r.ok ? mismatch : (int32_t) llama_persona_move::io_error;
}
// every check passed: bind it to her restored cells, as a take would have. Both allocations come first (a refusal
// here leaves her cells and the checkpoint as they were); what follows reserves nothing and cannot throw.
llama_kv_cells::checkpoint_position mark = std::make_shared<const llama_pos>(pos);
b.kv.cells.prepare_checkpoint();
b.kv.cells.mark_checkpoint(mark);
c.seq = b.seq;
c.pos = pos;
c.owner = llama_persona_identity { owner, epoch };
c.lineage = b.kv.cells.lineage();
c.mark = std::move(mark);
c.rs_layout = layout;
c.rs.has_state = has_state;
c.rs.pos = rs_pos;
c.rs.rs_idx = rs_idx;
c.rs.r.swap(rs_r);
c.rs.s.swap(rs_s);
c.taken = true;
return (int32_t) llama_persona_move::done;
}

// The exported boundary of both (Codex on #164): every allocation inside them (the path's staging name, row lengths from
// the geometry, the rows read, the receipt) may throw std::bad_alloc under memory pressure, and nothing may cross the C
// ABI into the caller. A refused write leaves no file of hers changed (the staging file is the only one opened, and only
// after its name is allocated); a refused read leaves nothing taken and her cells as they were (its commit allocates first).
int32_t llama_persona_checkpoint_write_file(const llama_persona * persona, const llama_persona_checkpoint * checkpoint, const char * path, uint64_t generation) {
try {
return checkpoint_write_file(persona, checkpoint, path, generation);
} catch (const std::bad_alloc &) {
return (int32_t) llama_persona_move::out_of_memory;
}
}

int32_t llama_persona_checkpoint_read_file(llama_persona * persona, const char * path, uint64_t generation, llama_persona_checkpoint * out) {
try {
return checkpoint_read_file(persona, path, generation, out);
} catch (const std::bad_alloc &) {
out->blocks.taken = false;
return (int32_t) llama_persona_move::out_of_memory;
}
}
6 changes: 6 additions & 0 deletions src/llama-persona.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,9 @@ struct llama_persona {
llama_persona_blocks blocks;
std::unique_ptr<llama_persona_tokens> tokens; // her token history, beside her blocks (llama-persona-tokens.h)
};

// a checkpoint of a bound mind (llama_persona_checkpoint_take): her recurrent rows and where she was, never her KV; its
// file form beside her record (llama-persona-file.cpp) carries it across a restart
struct llama_persona_checkpoint {
llama_persona_checkpoint_blocks blocks;
};
66 changes: 66 additions & 0 deletions tests/test-persona-blocks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -723,6 +723,72 @@ int main(int argc, char ** argv) {
std::remove(path.c_str());
}

// 10. what this catches (CONTINUUM-ENGINE 4.15 + 4.16; the M5, 2026-11-27: Aiko restored with 214,911 of 246,764
// tokens still hers re-prefilled 1,019 s, her checkpoint gone with the process): her checkpoint written beside her
// record and read into the blocks restored from it rewinds her so a different continuation is byte-identical to
// decoding it in place. Another stop's generation is refused; so is a write for blocks that are not hers. An
// allocation failure inside the read or the write returns out_of_memory across the C boundary (Codex on #164),
// leaves the checkpoint untaken and writes no file, and she still binds and serves.
{
auto st = [](int32_t s) { return (llama_persona_move) s; };
const std::string record = (std::filesystem::temp_directory_path() / "test-persona-checkpoint.cpb").string();
const std::string kept = (std::filesystem::temp_directory_path() / "test-persona-checkpoint.ckp").string();
const std::string unwritten = kept + ".refused";
const llama_token other = 9 % f.n_vocab;
llama_memory_clear(llama_get_memory(f.ctx), true);
f.prompt(0, 1);
const std::vector<float> 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_take(f.ctx, 0, 0xA, 1, cp)), llama_persona_move::done, "her checkpoint at her prompt's end");
f.decode_one(0, 3 % f.n_vocab, f.n_prompt); // she goes on past it before the stop
llama_persona * saved = llama_persona_init(f.ctx, 0xA, 1);
llama_persona * stranger = llama_persona_init(f.ctx, 0xB, 1);
require(saved != nullptr && stranger != nullptr, "personas to write");
require_move(st(llama_persona_save(f.ctx, 0, saved)), llama_persona_move::done, "she parks at the stop");
require_move(st(llama_persona_write_file(saved, record.c_str(), 13)), llama_persona_move::done, "her record");
require_move(st(llama_persona_checkpoint_write_file(stranger, cp, kept.c_str(), 13)), llama_persona_move::wrong_owner, "never written for blocks that are not hers");
g_fail_alloc = true;
const auto write_refused = st(llama_persona_checkpoint_write_file(saved, cp, unwritten.c_str(), 13));
g_fail_alloc = false;
require_move(write_refused, llama_persona_move::out_of_memory, "a write that cannot allocate refuses");
require(!std::filesystem::exists(unwritten) && !std::filesystem::exists(unwritten + ".tmp"), "and writes no file");
require_move(st(llama_persona_checkpoint_write_file(saved, cp, kept.c_str(), 13)), llama_persona_move::done, "her checkpoint beside her record");
llama_persona_free(saved);
llama_persona_free(stranger);
llama_persona_checkpoint_free(cp);

// the restart: a fresh persona from her record, then her checkpoint into it
llama_persona * back = llama_persona_init(f.ctx, 0xA, 1);
llama_persona_checkpoint * restored = llama_persona_checkpoint_init();
require(back != nullptr && restored != nullptr, "a fresh persona and checkpoint");
require_move(st(llama_persona_checkpoint_read_file(back, kept.c_str(), 13, restored)), llama_persona_move::file_mismatch, "nothing to bind it to before her record is read");
require_move(st(llama_persona_read_file(back, record.c_str(), 13)), llama_persona_move::done, "her record back");
require_move(st(llama_persona_checkpoint_read_file(back, kept.c_str(), 14, restored)), llama_persona_move::file_mismatch, "another stop's generation is refused");
require(llama_persona_checkpoint_pos(restored) == -1, "and leaves nothing taken");
g_fail_alloc = true;
const auto read_refused = st(llama_persona_checkpoint_read_file(back, kept.c_str(), 13, restored));
g_fail_alloc = false;
require_move(read_refused, llama_persona_move::out_of_memory, "a read that cannot allocate refuses");
require(llama_persona_checkpoint_pos(restored) == -1, "and leaves nothing taken");
fprintf(stderr, "checkpoint file, every allocation failing: write %s (no file written), read %s (taken position %d)\n",
llama_persona_move_name(write_refused), llama_persona_move_name(read_refused), (int) llama_persona_checkpoint_pos(restored));
require_move(st(llama_persona_checkpoint_read_file(back, kept.c_str(), 13, restored)), llama_persona_move::done, "her checkpoint back");
require(llama_persona_checkpoint_pos(restored) == f.n_prompt - 1, "it holds through her last prompt position");
llama_memory_clear(llama_get_memory(f.ctx), true);
require_move(st(llama_persona_bind(f.ctx, 0, back)), llama_persona_move::done, "she binds from her record");
require_move(st(llama_persona_checkpoint_rewind(f.ctx, 0, 0xA, 1, restored)), llama_persona_move::done, "she rewinds to her restored checkpoint");
require(f.pos_max(0) == f.n_prompt - 1, "what she held past it is cut");
require_identical("a different continuation after a restart", in_place, f.decode_one(0, other, f.n_prompt));
llama_persona_checkpoint_free(restored);
llama_persona_free(back);
std::remove(record.c_str());
std::remove(kept.c_str());
}

fprintf(stderr, "test-persona-blocks: ok (%zu attention layers, %zu recurrent layers)\n", a.kv.k.size(), a.rs.r.size());
return 0;
}
Loading