From 4ac41f7d43a3b2ffeb55ac881101f8d88757b48d Mon Sep 17 00:00:00 2001 From: Joel Teply Date: Mon, 16 Nov 2026 10:55:27 -0600 Subject: [PATCH] server: a request that names no view end keeps the mind's view end inside the reused prefix (~100k-token re-prefills on the M5) Measured on the live M5 (engine b5534b5, llama-server-58057.log, 2026-11-16): of ~8.3M tokens prefilled, 6.4M came after a successful checkpoint restore that resumed far behind the matched prefix (e.g. lcp 109,197, resumed at the head checkpoint 9,655, 99.5k tokens re-prefilled). Cause: a short side request (one token, resending the mind's prompt, no view_messages) overwrote slot.prompt.view_end with -1, so the page saved next kept only the head and the prompt's last checkpoint, and the mind's next prompt diverged just before that one. 83 of the 95 pages saved without a view end followed a <=8-token request. carried_view_end (server-view-end.h): a request's own view end wins; one that names none keeps the previous view end while it lies inside the prefix this request reused; otherwise -1. Receipts (qwen35-0.8b, M5): test-server-view-end ok. End to end (a view_messages request, a 1-token side request without one, then a page save): pre-fix "view end = -1", fixed "view end = 993", same build, only this line differing. 0 build warnings. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01LoTjvf5j3Ez13g6k8mRkFo --- tests/CMakeLists.txt | 1 + tests/test-server-view-end.cpp | 25 +++++++++++++++++++++++++ tools/server/server-context.cpp | 3 ++- tools/server/server-view-end.h | 18 ++++++++++++++++++ 4 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 tests/test-server-view-end.cpp create mode 100644 tools/server/server-view-end.h diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 4e1fd12b03dc..10ec890fe2af 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -181,6 +181,7 @@ if (LLAMA_LLGUIDANCE) endif () llama_build(test-recurrent-state-rollback.cpp) +llama_build_and_test(test-server-view-end.cpp) # GGML_TENSOR_FLAG_GRAD: a leaf whose gradient the backward computes, never optimized (the walk reads the cached prefix K/V gradient) llama_build_and_test(test-grad-leaf.cpp) diff --git a/tests/test-server-view-end.cpp b/tests/test-server-view-end.cpp new file mode 100644 index 000000000000..f3024aa01050 --- /dev/null +++ b/tests/test-server-view-end.cpp @@ -0,0 +1,25 @@ +// what this catches: a request that names no view end (a short side request resending a mind's prompt) +// erasing that mind's resume point, so its saved page drops the view-end checkpoint and its next act +// re-prefills from the head (regression, M5 engine b5534b5, 2026-11-16: 83 of 95 such pages; ~100k-token +// re-prefills). A named view end always wins; an old one survives only inside the reused prefix. +#include "../tools/server/server-view-end.h" + +#include +#include + +static void require(bool condition, const char * message) { + if (!condition) { + std::cerr << "FAILED: " << message << '\n'; + std::exit(1); + } +} + +int main() { + require(carried_view_end(120, 90, 100) == 120, "a request's own view end always wins"); + require(carried_view_end(-1, 109190, 109200) == 109190, "a side request that reused the prefix keeps the mind's view end"); + require(carried_view_end(-1, 109190, 109190) == 109190, "the view end exactly at the reused boundary survives"); + require(carried_view_end(-1, 109190, 5000) == -1, "a request that diverged before the old view end drops it"); + require(carried_view_end(-1, -1, 109200) == -1, "nothing known stays unknown"); + std::cout << "test-server-view-end: ok\n"; + return 0; +} diff --git a/tools/server/server-context.cpp b/tools/server/server-context.cpp index ba7b2093053a..af5d3fff080e 100644 --- a/tools/server/server-context.cpp +++ b/tools/server/server-context.cpp @@ -1,5 +1,6 @@ #include #include "server-context.h" +#include "server-view-end.h" #include "../../src/llama-ext.h" #include "server-train.h" #include "server-adapter-file.h" @@ -4051,7 +4052,7 @@ struct server_context_impl { // saved page keeps (SLOT_SAVE). They describe the prompt these tokens now hold, // so they travel with it (server_prompt). const auto view_end_pos = slot.task->params.view_end_pos; - slot.prompt.view_end = view_end_pos; + slot.prompt.view_end = carried_view_end(view_end_pos, slot.prompt.view_end, slot.prompt.n_tokens()); slot.prompt.end_from = std::max(0, slot.task->n_tokens() - std::min(n_batch, 4 + n_ubatch)); // add prompt tokens for processing in the current batch diff --git a/tools/server/server-view-end.h b/tools/server/server-view-end.h new file mode 100644 index 000000000000..0d1cb629dcc0 --- /dev/null +++ b/tools/server/server-view-end.h @@ -0,0 +1,18 @@ +#pragma once + +#include + +// Continuum fork: the view end a slot's tokens carry after a prompt pass that starts at `reused` (the +// prefix this request reused from the slot). A request that names its own view end sets it. One that +// names none (a short side request resending the mind's prompt, e.g. a one-token probe) keeps the +// previous view end while that point still lies inside the reused prefix: the mind's resume point is +// still in these tokens, and the page saved next must keep its checkpoint. Measured on the M5 (engine +// b5534b5, 2026-11-16): 83 of 95 pages that kept no view end were saved right after a <=8-token request, +// and the next act re-prefilled ~100k tokens from the head checkpoint (CONTINUUM-ENGINE 4.7.15). +// Otherwise the view end is unknown (-1). +inline int32_t carried_view_end(int32_t requested, int32_t previous, int32_t reused) { + if (requested >= 0) { + return requested; + } + return previous >= 0 && previous <= reused ? previous : -1; +}