From ae0500d7eed2d6b7cef41d5f9c58567f1b16fc0e Mon Sep 17 00:00:00 2001 From: Joel Teply Date: Tue, 17 Nov 2026 14:10:48 -0600 Subject: [PATCH 1/5] server: a request that continues her last response is served from the tokens her slot holds Her slot holds [the prompt she was served][the tokens she generated]: her reasoning, words and call as sampled. Her next request re-renders that response from its parsed message, without her reasoning, so its tokens differ from hers at her own response; the hybrid's recurrent state cannot truncate there and rewinds to a checkpoint (the M5, 2026-11-17: reuse_split gap 2,048 on every act). A request naming the response it continues (`continues`: the completion id her last response carried) is served as her held tokens plus the request's tokens from the end-of-turn that closes that response. Every condition (same response, her history held, she generated, an end of turn) is checked; a miss is said with its reason and the request is served as sent. Pure logic in server-continuation.h, proven by test-server-continuation (7 cases). CONTINUUM-ENGINE 4.14 slice (b). Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01LoTjvf5j3Ez13g6k8mRkFo --- tests/CMakeLists.txt | 4 + tests/test-server-continuation.cpp | 91 ++++++++++++++++++ tools/server/server-context.cpp | 57 ++++++++++++ tools/server/server-continuation.h | 142 +++++++++++++++++++++++++++++ tools/server/server-task.h | 4 + 5 files changed, 298 insertions(+) create mode 100644 tests/test-server-continuation.cpp create mode 100644 tools/server/server-continuation.h diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 408d42e577dd..df231045e8ba 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -189,6 +189,10 @@ endif () llama_build(test-recurrent-state-rollback.cpp) llama_build_and_test(test-server-view-end.cpp) +# a persona's next request continues the tokens her slot holds (CONTINUUM-ENGINE 4.14) +llama_build_and_test(test-server-continuation.cpp) +target_include_directories(test-server-continuation PRIVATE ${PROJECT_SOURCE_DIR}/tools/server) + # 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-continuation.cpp b/tests/test-server-continuation.cpp new file mode 100644 index 000000000000..87d26fe9b6a6 --- /dev/null +++ b/tests/test-server-continuation.cpp @@ -0,0 +1,91 @@ +// A persona's next request continues the tokens her slot holds (tools/server/server-continuation.h). +// Synthetic tokens: 1 = <|im_start|>user, 2 = <|im_start|>assistant, 9 = <|im_end|> (end of turn), +// 3 = "\n", 7 = her generation prompt's , 40.. = words. + +#include "server-continuation.h" + +#include +#include + +static bool is_eot(llama_token t) { return t == 9; } + +static common_chat_msg_spans spans_of(const llama_tokens & tokens) { + common_chat_msg_spans spans; + std::vector> at; + for (size_t i = 0; i < tokens.size(); i++) { + if (tokens[i] == 1) at.emplace_back(COMMON_CHAT_ROLE_USER, i); + if (tokens[i] == 2) at.emplace_back(COMMON_CHAT_ROLE_ASSISTANT, i); + } + for (size_t k = 0; k < at.size(); k++) { + const size_t end = k + 1 < at.size() ? at[k + 1].second : tokens.size(); + spans.add(at[k].first, at[k].second, end - at[k].second); + } + return spans; +} + +#define CHECK(cond) do { if (!(cond)) { fprintf(stderr, "%s:%d: %s\n", __FILE__, __LINE__, #cond); exit(1); } } while (0) + +int main() { + // The prompt she was served: a user turn, then her generation prompt. + const llama_tokens served = { 1, 40, 41, 9, 3, 2, 3, 7 }; + // What she generated after it: her reasoning (50, 51), her words (60), her call (61). No end of turn. + llama_tokens held = served; + held.insert(held.end(), { 50, 51, 60, 61 }); + const std::string response = "chatcmpl-a"; + + // Her next request re-renders her response WITHOUT her reasoning (60, 61), then her result and the new turn. + const llama_tokens sent = { 1, 40, 41, 9, 3, 2, 3, 60, 61, 9, 3, 1, 70, 71, 9, 3, 2, 3, 7 }; + const auto spans = spans_of(sent); + + // what this catches: her generated tokens (her reasoning) discarded and recomputed every act. + { + const auto c = continue_from({ held, served.size(), response }, { response, sent, spans }, is_eot); + CHECK(c.miss == continuation_miss::none); + llama_tokens expect = held; + expect.insert(expect.end(), { 9, 3, 1, 70, 71, 9, 3, 2, 3, 7 }); + CHECK(c.tokens == expect); + CHECK(c.from == 9); // the end of turn closing her response + CHECK(c.shift == (int64_t) held.size() - 9); + // A position past her response moves with the tail; one inside it is unknown; one before is kept. + CHECK(continued_position(c, 11) == 11 + c.shift); + CHECK(continued_position(c, 7) == -1); + CHECK(continued_position(c, 3) == 3); + } + // what this catches: a request naming no response is served as sent. + { + const std::string none; + CHECK(continue_from({ held, served.size(), response }, { none, sent, spans }, is_eot).miss == continuation_miss::not_requested); + } + // what this catches: another response's tokens standing in for the one the request carries. + { + const std::string other = "chatcmpl-b"; + CHECK(continue_from({ held, served.size(), response }, { other, sent, spans }, is_eot).miss == continuation_miss::other_response); + } + // what this catches: a slot that holds someone else's history (a different first turn) used as hers. + { + llama_tokens theirs = held; + theirs[1] = 99; + CHECK(continue_from({ theirs, served.size(), response }, { response, sent, spans }, is_eot).miss == continuation_miss::not_hers); + } + // what this catches: a slot that served the prompt but generated nothing being treated as holding her response. + { + CHECK(continue_from({ served, served.size(), response }, { response, sent, spans }, is_eot).miss == continuation_miss::not_generated); + } + // what this catches: the generation prompt (the request's last span) taken for her response. + { + const llama_tokens asking = { 1, 40, 41, 9, 3, 2, 3, 7 }; + CHECK(continue_from({ held, served.size(), response }, { response, asking, spans_of(asking) }, is_eot).miss == continuation_miss::no_response); + } + // what this catches: a request with no assistant message spliced anyway. + { + const llama_tokens no_reply = { 1, 40, 41, 9, 3 }; + CHECK(continue_from({ held, served.size(), response }, { response, no_reply, spans_of(no_reply) }, is_eot).miss == continuation_miss::no_response); + } + // what this catches: a template whose response carries no end of turn spliced at a guessed point. + { + const llama_tokens open = { 1, 40, 41, 9, 3, 2, 3, 60, 61, 1, 70, 2, 3 }; + CHECK(continue_from({ held, served.size(), response }, { response, open, spans_of(open) }, is_eot).miss == continuation_miss::no_end_of_turn); + } + fprintf(stderr, "test-server-continuation: ok\n"); + return 0; +} diff --git a/tools/server/server-context.cpp b/tools/server/server-context.cpp index 15831cececbc..556565db3ec5 100644 --- a/tools/server/server-context.cpp +++ b/tools/server/server-context.cpp @@ -1,6 +1,7 @@ #include #include "server-context.h" #include "server-checkpoint-policy.h" +#include "server-continuation.h" #include "server-view-end.h" #include "../../src/llama-ext.h" #include "server-train.h" @@ -302,6 +303,12 @@ struct server_slot { std::string generated_text; std::string debug_generated_text; llama_tokens generated_tokens; + + // Continuum fork: her last response, for the request that continues it (server-continuation.h): + // its completion id and how many of `prompt.tokens` were the prompt she was served for it. Kept + // across release; her held tokens are checked against the request, never trusted by id alone. + std::string served_response; + size_t n_served = 0; size_t n_sent_text = 0; // number of sent text character (i.e. handle partial UTF-8 on streaming) std::vector generated_token_probs; @@ -1924,6 +1931,46 @@ struct server_context_impl { return output; } + // A request that continues her last response is served from the tokens her slot holds: her + // generated tokens stay, and only what follows her response is new (server-continuation.h). + // Runs after the adapter check, so a slot cleared for an adapter change never continues. + void continue_her_response(server_slot & slot, server_task & task) { + if (task.params.continues.empty()) { + return; + } + if (task.tokens.has_mtmd || slot.prompt.tokens.has_mtmd) { + SLT_WRN(slot, "continuation of %s missed (%s): served as sent\n", task.params.continues.c_str(), + continuation_miss_name(continuation_miss::has_media)); + return; + } + const llama_tokens & held = slot.prompt.tokens.get_tokens(); + const llama_tokens & sent = task.tokens.get_tokens(); + const continuation c = continue_from( + continuation_held{ held, slot.n_served, slot.served_response }, + continuation_request{ task.params.continues, sent, task.params.message_spans }, + [this](llama_token tok) { return llama_vocab_is_eog(vocab, tok); }); + if (c.miss == continuation_miss::not_requested) { + return; + } + if (c.miss != continuation_miss::none) { + SLT_WRN(slot, "continuation of %s missed (%s): served as sent, her held tokens past the shared prefix are recomputed\n", + task.params.continues.c_str(), continuation_miss_name(c.miss)); + return; + } + for (auto & span : task.params.message_spans.spans) { + if (span.pos >= c.from) { + span.pos += c.shift; + } else if (span.pos + span.len > c.from) { + span.len += c.shift; // her response's span now ends where her held tokens and the glue do + } + } + task.params.view_end_pos = (int32_t) continued_position(c, task.params.view_end_pos); + SLT_INF(slot, "continuing %s: %zu held tokens (%zu generated) kept, %zu new, %zu re-rendered tokens of her response not sent\n", + task.params.continues.c_str(), held.size(), held.size() - slot.n_served, + sent.size() - c.from, c.from - c.response); + task.tokens = server_tokens(c.tokens, false); + } + bool launch_slot_with_task(server_slot & slot, server_task && task) { page_slot_reassigned(slot); // a restore buffered for this slot is dropped, never applied over it (server-context-page.inc) // process per-request lora adapters @@ -1945,6 +1992,8 @@ struct server_context_impl { } } + continue_her_response(slot, task); + // if using alora, make sure it's only a single one requested and active size_t alora_invocation_start = task.tokens.size(); if (lora_all_alora(slot.lora)) { @@ -2324,6 +2373,13 @@ struct server_context_impl { } auto res = std::make_unique(); + // Her slot now holds this response after the prompt she was served: the next request that + // names it continues from these tokens (server-continuation.h). + if (!slot.task->is_child()) { + slot.served_response = slot.task->params.oaicompat_cmpl_id; + slot.n_served = slot.task->n_tokens(); + } + res->id = slot.task->id; res->id_slot = slot.id; res->learned = slot.learned; @@ -4981,6 +5037,7 @@ std::unique_ptr server_routes::handle_completions_impl( data); task.params.message_spans = task.tokens.find_message_spans(delimiters); + task.params.continues = json_value(data, "continues", std::string()); // Continuum fork: where the caller's view ends, so the prompt is checkpointed there. if (data.contains("view_messages") && data.contains("messages")) { std::string refusal; diff --git a/tools/server/server-continuation.h b/tools/server/server-continuation.h new file mode 100644 index 000000000000..2a2207b1cc18 --- /dev/null +++ b/tools/server/server-continuation.h @@ -0,0 +1,142 @@ +#pragma once + +#include "chat.h" +#include "common.h" +#include "llama.h" + +#include +#include +#include +#include +#include + +// Continuum fork: a persona's next request continues the tokens her slot already holds, never +// re-rendered, re-tokenized or recomputed (CONTINUUM-ENGINE 4.14, "her token history is the +// engine's"). +// +// After her act her slot holds [the prompt she was served][the tokens she generated]: her reasoning, +// her words and her call, exactly as sampled, without the end-of-turn token that stopped her. Her +// next request re-renders that response from its parsed message, without her reasoning, so its tokens +// differ from hers at her own response; a hybrid model's recurrent state cannot truncate there and +// rewinds to a checkpoint (the M5, 2026-11-17: 2,048 tokens recomputed on every act). A request that +// names the response it continues (`continues`, the completion id her last response carried) is +// instead served as her held tokens followed by the request's tokens from the end-of-turn that closes +// that response: the template's own glue, then everything new. +// +// Anything that does not hold exactly is a typed miss, said by the caller: the request is then served +// as sent. + +enum class continuation_miss { + none, // continued + not_requested, // the request names no response it continues + other_response, // her slot's last response is not the one the request continues + has_media, // a multimodal prompt is not spliced (checked by the caller: its tokens are not plain) + no_response, // the request holds no assistant message to continue after + not_hers, // her slot does not hold the request's history up to that response + not_generated, // her slot generated nothing after the prompt she was served + no_end_of_turn, // the response's rendering carries no end-of-turn token +}; + +inline const char * continuation_miss_name(continuation_miss miss) { + switch (miss) { + case continuation_miss::none: return "none"; + case continuation_miss::not_requested: return "not_requested"; + case continuation_miss::other_response: return "other_response"; + case continuation_miss::has_media: return "has_media"; + case continuation_miss::no_response: return "no_response"; + case continuation_miss::not_hers: return "not_hers"; + case continuation_miss::not_generated: return "not_generated"; + case continuation_miss::no_end_of_turn: return "no_end_of_turn"; + } + return "unknown"; +} + +// What her slot holds from her last response. +struct continuation_held { + const llama_tokens & tokens; // the prompt she was served, then what she generated + size_t n_served; // how many of `tokens` are the prompt she was served + const std::string & response; // the completion id that response carried +}; + +// The request as rendered and tokenized. +struct continuation_request { + const std::string & continues; // the completion id it continues, or empty + const llama_tokens & tokens; + const common_chat_msg_spans & spans; // its messages' token spans +}; + +struct continuation { + continuation_miss miss = continuation_miss::not_requested; + llama_tokens tokens; // her held tokens, then the request's from the response's end-of-turn + size_t response = 0; // where her response starts in the request's tokens + size_t from = 0; // where in the request's tokens the kept tail starts + int64_t shift = 0; // how far every request position at or after `from` moved +}; + +inline continuation continue_from(const continuation_held & held, const continuation_request & request, + const std::function & is_end_of_turn) { + continuation out; + if (request.continues.empty()) { + return out; + } + if (request.continues != held.response) { + out.miss = continuation_miss::other_response; + return out; + } + // Her response is the request's newest assistant message that something follows: everything after + // it is new. The request's own last span is the generation prompt for her next turn, never it. + const common_chat_msg_span * response = nullptr; + const auto & spans = request.spans.spans; + for (size_t i = 0; i + 1 < spans.size(); i++) { + if (spans[i].role == COMMON_CHAT_ROLE_ASSISTANT) { + response = &spans[i]; + } + } + if (response == nullptr) { + out.miss = continuation_miss::no_response; + return out; + } + // Her slot holds the request's history up to her response: the prompt she was served began with it. + const size_t start = response->pos; + if (held.n_served < start || held.tokens.size() < start || + !std::equal(request.tokens.begin(), request.tokens.begin() + start, held.tokens.begin())) { + out.miss = continuation_miss::not_hers; + return out; + } + if (held.tokens.size() <= held.n_served) { + out.miss = continuation_miss::not_generated; + return out; + } + // The end-of-turn that closes her response in the request: what her generation stopped on. + size_t end_of_turn = start + response->len; + for (size_t i = start + response->len; i > start; --i) { + if (is_end_of_turn(request.tokens[i - 1])) { + end_of_turn = i - 1; + break; + } + } + if (end_of_turn == start + response->len) { + out.miss = continuation_miss::no_end_of_turn; + return out; + } + out.miss = continuation_miss::none; + out.response = start; + out.from = end_of_turn; + out.shift = (int64_t) held.tokens.size() - (int64_t) end_of_turn; + out.tokens.reserve(held.tokens.size() + request.tokens.size() - end_of_turn); + out.tokens.insert(out.tokens.end(), held.tokens.begin(), held.tokens.end()); + out.tokens.insert(out.tokens.end(), request.tokens.begin() + end_of_turn, request.tokens.end()); + return out; +} + +// A request position after the splice: unchanged before the kept tail, moved by `shift` within it, +// and unknown (-1) inside her response, which her held tokens replace. +inline int64_t continued_position(const continuation & c, int64_t pos) { + if (pos < 0 || c.miss != continuation_miss::none || (size_t) pos <= c.response) { + return pos; + } + if ((size_t) pos < c.from) { + return -1; + } + return pos + c.shift; +} diff --git a/tools/server/server-task.h b/tools/server/server-task.h index c757135afcc0..e5d377ccdb80 100644 --- a/tools/server/server-task.h +++ b/tools/server/server-task.h @@ -175,6 +175,10 @@ struct task_params { // is checkpointed exactly there and a saved page keeps that checkpoint. -1 = not given. int32_t view_end_pos = -1; + // Continuum fork: the completion id of the response this request continues (`continues`): her + // slot's held tokens stand for that response and everything before it (server-continuation.h). + std::string continues; + // Embeddings int32_t embd_normalize = 2; // (-1=none, 0=max absolute int16, 1=taxicab, 2=Euclidean/L2, >2=p-norm) From 7f6622121a0e791cef9ac18a567234db740a810e Mon Sep 17 00:00:00 2001 From: Joel Teply Date: Tue, 17 Nov 2026 14:11:46 -0600 Subject: [PATCH 2/5] server: requests are named by the caller (request_id) and continue by that name, never by an id this server minted Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01LoTjvf5j3Ez13g6k8mRkFo --- tools/server/server-context.cpp | 5 +++-- tools/server/server-continuation.h | 6 +++--- tools/server/server-task.h | 6 ++++-- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/tools/server/server-context.cpp b/tools/server/server-context.cpp index 556565db3ec5..f00035405974 100644 --- a/tools/server/server-context.cpp +++ b/tools/server/server-context.cpp @@ -305,7 +305,7 @@ struct server_slot { llama_tokens generated_tokens; // Continuum fork: her last response, for the request that continues it (server-continuation.h): - // its completion id and how many of `prompt.tokens` were the prompt she was served for it. Kept + // the caller's id for that request and how many of `prompt.tokens` were the prompt she was served. Kept // across release; her held tokens are checked against the request, never trusted by id alone. std::string served_response; size_t n_served = 0; @@ -2376,7 +2376,7 @@ struct server_context_impl { // Her slot now holds this response after the prompt she was served: the next request that // names it continues from these tokens (server-continuation.h). if (!slot.task->is_child()) { - slot.served_response = slot.task->params.oaicompat_cmpl_id; + slot.served_response = slot.task->params.request_id; slot.n_served = slot.task->n_tokens(); } @@ -5037,6 +5037,7 @@ std::unique_ptr server_routes::handle_completions_impl( data); task.params.message_spans = task.tokens.find_message_spans(delimiters); + task.params.request_id = json_value(data, "request_id", std::string()); task.params.continues = json_value(data, "continues", std::string()); // Continuum fork: where the caller's view ends, so the prompt is checkpointed there. if (data.contains("view_messages") && data.contains("messages")) { diff --git a/tools/server/server-continuation.h b/tools/server/server-continuation.h index 2a2207b1cc18..44e3a0831cf7 100644 --- a/tools/server/server-continuation.h +++ b/tools/server/server-continuation.h @@ -19,7 +19,7 @@ // next request re-renders that response from its parsed message, without her reasoning, so its tokens // differ from hers at her own response; a hybrid model's recurrent state cannot truncate there and // rewinds to a checkpoint (the M5, 2026-11-17: 2,048 tokens recomputed on every act). A request that -// names the response it continues (`continues`, the completion id her last response carried) is +// names the response it continues (`continues`, the caller's id for the request her last response answered) is // instead served as her held tokens followed by the request's tokens from the end-of-turn that closes // that response: the template's own glue, then everything new. // @@ -55,12 +55,12 @@ inline const char * continuation_miss_name(continuation_miss miss) { struct continuation_held { const llama_tokens & tokens; // the prompt she was served, then what she generated size_t n_served; // how many of `tokens` are the prompt she was served - const std::string & response; // the completion id that response carried + const std::string & response; // the caller's id for the request that response answered }; // The request as rendered and tokenized. struct continuation_request { - const std::string & continues; // the completion id it continues, or empty + const std::string & continues; // the id of the request whose response it continues, or empty const llama_tokens & tokens; const common_chat_msg_spans & spans; // its messages' token spans }; diff --git a/tools/server/server-task.h b/tools/server/server-task.h index e5d377ccdb80..b3ff51ad2f26 100644 --- a/tools/server/server-task.h +++ b/tools/server/server-task.h @@ -175,8 +175,10 @@ struct task_params { // is checkpointed exactly there and a saved page keeps that checkpoint. -1 = not given. int32_t view_end_pos = -1; - // Continuum fork: the completion id of the response this request continues (`continues`): her - // slot's held tokens stand for that response and everything before it (server-continuation.h). + // Continuum fork: the caller's own id for this request (`request_id`), and the id of the request + // whose response this one continues (`continues`): her slot's held tokens stand for that response + // and everything before it (server-continuation.h). Named by the caller, never by this server. + std::string request_id; std::string continues; // Embeddings From f79e6e447ec392e43974c8a097a2bbbfea27ec58 Mon Sep 17 00:00:00 2001 From: Joel Teply Date: Tue, 17 Nov 2026 14:13:59 -0600 Subject: [PATCH 3/5] server: her slot keeps the lineage of responses it holds, so a warm pass during her tool and her next request both continue her response A continuation keeps every earlier response as its prefix; any other task clears the lineage at launch, so it never outlives the tokens it describes. A splice keeps her tokens only up to the continued response's end (the prompt cache finds what follows). The warm pass that today re-sends the prompt she was served truncates her generated tokens out of her KV; continuing her response instead keeps them. test-server-continuation: 12 checks incl. the warm pass. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01LoTjvf5j3Ez13g6k8mRkFo --- tests/test-server-continuation.cpp | 63 ++++++++++++++++-------------- tools/server/server-context.cpp | 24 ++++++------ tools/server/server-continuation.h | 42 ++++++++++++++------ 3 files changed, 76 insertions(+), 53 deletions(-) diff --git a/tests/test-server-continuation.cpp b/tests/test-server-continuation.cpp index 87d26fe9b6a6..eb7650c9dd82 100644 --- a/tests/test-server-continuation.cpp +++ b/tests/test-server-continuation.cpp @@ -23,6 +23,12 @@ static common_chat_msg_spans spans_of(const llama_tokens & tokens) { return spans; } +static continuation run(const llama_tokens & held, const std::vector & responses, + const std::string & continues, const llama_tokens & sent) { + const auto spans = spans_of(sent); + return continue_from({ held, responses }, { continues, sent, spans }, is_eot); +} + #define CHECK(cond) do { if (!(cond)) { fprintf(stderr, "%s:%d: %s\n", __FILE__, __LINE__, #cond); exit(1); } } while (0) int main() { @@ -31,61 +37,60 @@ int main() { // What she generated after it: her reasoning (50, 51), her words (60), her call (61). No end of turn. llama_tokens held = served; held.insert(held.end(), { 50, 51, 60, 61 }); - const std::string response = "chatcmpl-a"; + const std::vector act = { { "act-1", served.size(), held.size() } }; // Her next request re-renders her response WITHOUT her reasoning (60, 61), then her result and the new turn. const llama_tokens sent = { 1, 40, 41, 9, 3, 2, 3, 60, 61, 9, 3, 1, 70, 71, 9, 3, 2, 3, 7 }; - const auto spans = spans_of(sent); // what this catches: her generated tokens (her reasoning) discarded and recomputed every act. { - const auto c = continue_from({ held, served.size(), response }, { response, sent, spans }, is_eot); + const auto c = run(held, act, "act-1", sent); CHECK(c.miss == continuation_miss::none); llama_tokens expect = held; expect.insert(expect.end(), { 9, 3, 1, 70, 71, 9, 3, 2, 3, 7 }); CHECK(c.tokens == expect); CHECK(c.from == 9); // the end of turn closing her response + CHECK(c.kept == held.size() && c.generated == 4); CHECK(c.shift == (int64_t) held.size() - 9); // A position past her response moves with the tail; one inside it is unknown; one before is kept. CHECK(continued_position(c, 11) == 11 + c.shift); CHECK(continued_position(c, 7) == -1); CHECK(continued_position(c, 3) == 3); } - // what this catches: a request naming no response is served as sent. - { - const std::string none; - CHECK(continue_from({ held, served.size(), response }, { none, sent, spans }, is_eot).miss == continuation_miss::not_requested); - } - // what this catches: another response's tokens standing in for the one the request carries. + // what this catches: a warm pass during her tool (one token generated after her response's glue) + // making her real request miss, or its token leaking into her prompt. Both continue act-1: the + // slot's lineage still holds act-1, and only act-1's tokens are kept; the cache finds the glue. { - const std::string other = "chatcmpl-b"; - CHECK(continue_from({ held, served.size(), response }, { other, sent, spans }, is_eot).miss == continuation_miss::other_response); + llama_tokens warmed = held; + warmed.insert(warmed.end(), { 9, 3, 2, 3, 7, 80 }); + const std::vector lineage = { act[0], { "warm-1", warmed.size() - 1, warmed.size() } }; + const auto c = run(warmed, lineage, "act-1", sent); + CHECK(c.miss == continuation_miss::none); + CHECK(c.kept == held.size()); + llama_tokens expect = held; + expect.insert(expect.end(), { 9, 3, 1, 70, 71, 9, 3, 2, 3, 7 }); + CHECK(c.tokens == expect); } + // what this catches: a request naming no response is served as sent. + CHECK(run(held, act, "", sent).miss == continuation_miss::not_requested); + // what this catches: another response's tokens standing in for the one the request continues. + CHECK(run(held, act, "act-2", sent).miss == continuation_miss::other_response); + // what this catches: a lineage that outlived its tokens (the slot was cut shorter than her response). + CHECK(run(served, act, "act-1", sent).miss == continuation_miss::other_response); // what this catches: a slot that holds someone else's history (a different first turn) used as hers. { llama_tokens theirs = held; theirs[1] = 99; - CHECK(continue_from({ theirs, served.size(), response }, { response, sent, spans }, is_eot).miss == continuation_miss::not_hers); - } - // what this catches: a slot that served the prompt but generated nothing being treated as holding her response. - { - CHECK(continue_from({ served, served.size(), response }, { response, sent, spans }, is_eot).miss == continuation_miss::not_generated); + CHECK(run(theirs, act, "act-1", sent).miss == continuation_miss::not_hers); } + // what this catches: a response that generated nothing treated as holding her response. + CHECK(run(held, { { "act-1", held.size(), held.size() } }, "act-1", sent).miss == continuation_miss::not_generated); + // what this catches: a response whose served prompt ended before the request's response begins. + CHECK(run(held, { { "act-1", 3, held.size() } }, "act-1", sent).miss == continuation_miss::not_hers); // what this catches: the generation prompt (the request's last span) taken for her response. - { - const llama_tokens asking = { 1, 40, 41, 9, 3, 2, 3, 7 }; - CHECK(continue_from({ held, served.size(), response }, { response, asking, spans_of(asking) }, is_eot).miss == continuation_miss::no_response); - } - // what this catches: a request with no assistant message spliced anyway. - { - const llama_tokens no_reply = { 1, 40, 41, 9, 3 }; - CHECK(continue_from({ held, served.size(), response }, { response, no_reply, spans_of(no_reply) }, is_eot).miss == continuation_miss::no_response); - } + CHECK(run(held, act, "act-1", { 1, 40, 41, 9, 3, 2, 3, 7 }).miss == continuation_miss::no_response); // what this catches: a template whose response carries no end of turn spliced at a guessed point. - { - const llama_tokens open = { 1, 40, 41, 9, 3, 2, 3, 60, 61, 1, 70, 2, 3 }; - CHECK(continue_from({ held, served.size(), response }, { response, open, spans_of(open) }, is_eot).miss == continuation_miss::no_end_of_turn); - } + CHECK(run(held, act, "act-1", { 1, 40, 41, 9, 3, 2, 3, 60, 61, 1, 70, 2, 3 }).miss == continuation_miss::no_end_of_turn); fprintf(stderr, "test-server-continuation: ok\n"); return 0; } diff --git a/tools/server/server-context.cpp b/tools/server/server-context.cpp index f00035405974..d0d423879bda 100644 --- a/tools/server/server-context.cpp +++ b/tools/server/server-context.cpp @@ -304,11 +304,10 @@ struct server_slot { std::string debug_generated_text; llama_tokens generated_tokens; - // Continuum fork: her last response, for the request that continues it (server-continuation.h): - // the caller's id for that request and how many of `prompt.tokens` were the prompt she was served. Kept - // across release; her held tokens are checked against the request, never trusted by id alone. - std::string served_response; - size_t n_served = 0; + // Continuum fork: the responses her slot's tokens hold, for the request that continues one + // (server-continuation.h). Valid only while every task since was a continuation of them: any other + // task clears it at launch, and a response is added at its final result. + std::vector responses; size_t n_sent_text = 0; // number of sent text character (i.e. handle partial UTF-8 on streaming) std::vector generated_token_probs; @@ -1936,9 +1935,11 @@ struct server_context_impl { // Runs after the adapter check, so a slot cleared for an adapter change never continues. void continue_her_response(server_slot & slot, server_task & task) { if (task.params.continues.empty()) { + slot.responses.clear(); // a new lineage: what this task does to her tokens is not a continuation return; } if (task.tokens.has_mtmd || slot.prompt.tokens.has_mtmd) { + slot.responses.clear(); SLT_WRN(slot, "continuation of %s missed (%s): served as sent\n", task.params.continues.c_str(), continuation_miss_name(continuation_miss::has_media)); return; @@ -1946,13 +1947,11 @@ struct server_context_impl { const llama_tokens & held = slot.prompt.tokens.get_tokens(); const llama_tokens & sent = task.tokens.get_tokens(); const continuation c = continue_from( - continuation_held{ held, slot.n_served, slot.served_response }, + continuation_held{ held, slot.responses }, continuation_request{ task.params.continues, sent, task.params.message_spans }, [this](llama_token tok) { return llama_vocab_is_eog(vocab, tok); }); - if (c.miss == continuation_miss::not_requested) { - return; - } if (c.miss != continuation_miss::none) { + slot.responses.clear(); SLT_WRN(slot, "continuation of %s missed (%s): served as sent, her held tokens past the shared prefix are recomputed\n", task.params.continues.c_str(), continuation_miss_name(c.miss)); return; @@ -1966,7 +1965,7 @@ struct server_context_impl { } task.params.view_end_pos = (int32_t) continued_position(c, task.params.view_end_pos); SLT_INF(slot, "continuing %s: %zu held tokens (%zu generated) kept, %zu new, %zu re-rendered tokens of her response not sent\n", - task.params.continues.c_str(), held.size(), held.size() - slot.n_served, + task.params.continues.c_str(), c.kept, c.generated, sent.size() - c.from, c.from - c.response); task.tokens = server_tokens(c.tokens, false); } @@ -2375,9 +2374,8 @@ struct server_context_impl { // Her slot now holds this response after the prompt she was served: the next request that // names it continues from these tokens (server-continuation.h). - if (!slot.task->is_child()) { - slot.served_response = slot.task->params.request_id; - slot.n_served = slot.task->n_tokens(); + if (!slot.task->is_child() && !slot.task->params.request_id.empty() && slot.stats.n_gen > 0) { + slot.responses.push_back({ slot.task->params.request_id, (size_t) slot.task->n_tokens(), (size_t) slot.prompt.n_tokens() }); } res->id = slot.task->id; diff --git a/tools/server/server-continuation.h b/tools/server/server-continuation.h index 44e3a0831cf7..9e2c75c19e4a 100644 --- a/tools/server/server-continuation.h +++ b/tools/server/server-continuation.h @@ -52,10 +52,18 @@ inline const char * continuation_miss_name(continuation_miss miss) { } // What her slot holds from her last response. +// One response her slot holds: the caller's id for the request it answered, where the prompt she was +// served for it ends and where her response ends in the slot's tokens. A slot holds a lineage of them: +// a continuation keeps every earlier one as its prefix, any other request starts a new lineage. +struct continuation_response { + std::string id; + size_t n_served = 0; + size_t n_end = 0; +}; + struct continuation_held { - const llama_tokens & tokens; // the prompt she was served, then what she generated - size_t n_served; // how many of `tokens` are the prompt she was served - const std::string & response; // the caller's id for the request that response answered + const llama_tokens & tokens; // her slot's tokens + const std::vector & responses; // the responses they hold, oldest first }; // The request as rendered and tokenized. @@ -68,7 +76,9 @@ struct continuation_request { struct continuation { continuation_miss miss = continuation_miss::not_requested; llama_tokens tokens; // her held tokens, then the request's from the response's end-of-turn - size_t response = 0; // where her response starts in the request's tokens + size_t response = 0; // where her response starts in the request's tokens + size_t kept = 0; // her held tokens kept: the prompt she was served and her response + size_t generated = 0; // of those, the tokens she generated size_t from = 0; // where in the request's tokens the kept tail starts int64_t shift = 0; // how far every request position at or after `from` moved }; @@ -79,7 +89,13 @@ inline continuation continue_from(const continuation_held & held, const continua if (request.continues.empty()) { return out; } - if (request.continues != held.response) { + const continuation_response * answered = nullptr; + for (const auto & r : held.responses) { + if (r.id == request.continues) { + answered = &r; + } + } + if (answered == nullptr || answered->n_end > held.tokens.size()) { out.miss = continuation_miss::other_response; return out; } @@ -98,12 +114,12 @@ inline continuation continue_from(const continuation_held & held, const continua } // Her slot holds the request's history up to her response: the prompt she was served began with it. const size_t start = response->pos; - if (held.n_served < start || held.tokens.size() < start || + if (answered->n_served < start || !std::equal(request.tokens.begin(), request.tokens.begin() + start, held.tokens.begin())) { out.miss = continuation_miss::not_hers; return out; } - if (held.tokens.size() <= held.n_served) { + if (answered->n_end <= answered->n_served) { out.miss = continuation_miss::not_generated; return out; } @@ -120,11 +136,15 @@ inline continuation continue_from(const continuation_held & held, const continua return out; } out.miss = continuation_miss::none; - out.response = start; + out.response = start; + out.kept = answered->n_end; + out.generated = answered->n_end - answered->n_served; out.from = end_of_turn; - out.shift = (int64_t) held.tokens.size() - (int64_t) end_of_turn; - out.tokens.reserve(held.tokens.size() + request.tokens.size() - end_of_turn); - out.tokens.insert(out.tokens.end(), held.tokens.begin(), held.tokens.end()); + // Her tokens up to her response's end: whatever the slot holds after it (a warm pass's glue) is + // found again by the prompt cache, never trusted here. + out.shift = (int64_t) answered->n_end - (int64_t) end_of_turn; + out.tokens.reserve(answered->n_end + request.tokens.size() - end_of_turn); + out.tokens.insert(out.tokens.end(), held.tokens.begin(), held.tokens.begin() + answered->n_end); out.tokens.insert(out.tokens.end(), request.tokens.begin() + end_of_turn, request.tokens.end()); return out; } From d0a70997f1987fa4909cdd109d089bb5d19b83ec Mon Sep 17 00:00:00 2001 From: Joel Teply Date: Tue, 17 Nov 2026 14:20:09 -0600 Subject: [PATCH 4/5] server: a continuation that does not hold is refused (continuation_refused, 409) and her held tokens and lineage are kept; never served as sent Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01LoTjvf5j3Ez13g6k8mRkFo --- tools/server/server-common.cpp | 4 ++++ tools/server/server-common.h | 1 + tools/server/server-context.cpp | 32 ++++++++++++++++++++------------ 3 files changed, 25 insertions(+), 12 deletions(-) diff --git a/tools/server/server-common.cpp b/tools/server/server-common.cpp index eadc271dc706..e603183fc6cd 100644 --- a/tools/server/server-common.cpp +++ b/tools/server/server-common.cpp @@ -48,6 +48,10 @@ json format_error_response(const std::string & message, const enum error_type ty type_str = "unavailable_error"; code = 503; break; + case ERROR_TYPE_CONTINUATION_REFUSED: + type_str = "continuation_refused"; + code = 409; + break; case ERROR_TYPE_EXCEED_CONTEXT_SIZE: type_str = "exceed_context_size_error"; code = 400; diff --git a/tools/server/server-common.h b/tools/server/server-common.h index 24bbb7a0bd57..61b7aebb49ca 100644 --- a/tools/server/server-common.h +++ b/tools/server/server-common.h @@ -61,6 +61,7 @@ enum error_type { ERROR_TYPE_UNAVAILABLE, // custom error ERROR_TYPE_NOT_SUPPORTED, // custom error ERROR_TYPE_EXCEED_CONTEXT_SIZE, // custom error + ERROR_TYPE_CONTINUATION_REFUSED, // Continuum fork: a request's continuation does not hold (server-continuation.h) }; // thin wrapper around common_grammar_trigger with (de)serialization functions diff --git a/tools/server/server-context.cpp b/tools/server/server-context.cpp index d0d423879bda..65ea729506f1 100644 --- a/tools/server/server-context.cpp +++ b/tools/server/server-context.cpp @@ -1932,17 +1932,25 @@ struct server_context_impl { // A request that continues her last response is served from the tokens her slot holds: her // generated tokens stay, and only what follows her response is new (server-continuation.h). - // Runs after the adapter check, so a slot cleared for an adapter change never continues. - void continue_her_response(server_slot & slot, server_task & task) { + // Runs after the adapter check, so a slot cleared for an adapter change never continues. A + // continuation that does not hold is REFUSED, typed, and her held tokens and lineage are kept: + // serving it as sent would recompute what she already holds. A cold start is the caller's own + // decision (a request that names no continuation). Returns false when it refused. + bool continue_her_response(server_slot & slot, server_task & task) { if (task.params.continues.empty()) { slot.responses.clear(); // a new lineage: what this task does to her tokens is not a continuation - return; + return true; } + const auto refuse = [&](continuation_miss miss) { + SLT_WRN(slot, "continuation of %s refused (%s): her held tokens are kept\n", task.params.continues.c_str(), + continuation_miss_name(miss)); + send_error(task, string_format("continuation of %s refused (%s): her slot keeps her held tokens; a cold start is a request that names no continuation", + task.params.continues.c_str(), continuation_miss_name(miss)), + ERROR_TYPE_CONTINUATION_REFUSED); + return false; + }; if (task.tokens.has_mtmd || slot.prompt.tokens.has_mtmd) { - slot.responses.clear(); - SLT_WRN(slot, "continuation of %s missed (%s): served as sent\n", task.params.continues.c_str(), - continuation_miss_name(continuation_miss::has_media)); - return; + return refuse(continuation_miss::has_media); } const llama_tokens & held = slot.prompt.tokens.get_tokens(); const llama_tokens & sent = task.tokens.get_tokens(); @@ -1951,10 +1959,7 @@ struct server_context_impl { continuation_request{ task.params.continues, sent, task.params.message_spans }, [this](llama_token tok) { return llama_vocab_is_eog(vocab, tok); }); if (c.miss != continuation_miss::none) { - slot.responses.clear(); - SLT_WRN(slot, "continuation of %s missed (%s): served as sent, her held tokens past the shared prefix are recomputed\n", - task.params.continues.c_str(), continuation_miss_name(c.miss)); - return; + return refuse(c.miss); } for (auto & span : task.params.message_spans.spans) { if (span.pos >= c.from) { @@ -1968,6 +1973,7 @@ struct server_context_impl { task.params.continues.c_str(), c.kept, c.generated, sent.size() - c.from, c.from - c.response); task.tokens = server_tokens(c.tokens, false); + return true; } bool launch_slot_with_task(server_slot & slot, server_task && task) { @@ -1991,7 +1997,9 @@ struct server_context_impl { } } - continue_her_response(slot, task); + if (!continue_her_response(slot, task)) { + return false; + } // if using alora, make sure it's only a single one requested and active size_t alora_invocation_start = task.tokens.size(); From 1f48f94bdf7221e0413f2475962ae948ad775233 Mon Sep 17 00:00:00 2001 From: Joel Teply Date: Tue, 17 Nov 2026 14:23:41 -0600 Subject: [PATCH 5/5] server: a continuing request carries only its new messages, rendered behind a stub of her response; never her history re-rendered, compared or copied The request names the response it continues and carries only the messages after it. The server renders them behind a short stub (a user line, an assistant turn whose tool calls the new tool results answer), never her response, and cuts at the stub's end of turn: the template's own glue that closes her turn, then the new messages. Her request is her lineage's held tokens up to that response's end plus the delta. No history comparison: the lineage names the response, and any non-continuation clears it. Misses (no stub delimiter, no end of turn, other response, not generated, media) are refused, typed (continuation_refused). test-server-continuation: three consecutive acts with reasoning the caller never sends (Codex's act-3 case), the warm-pass lineage, and every miss. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01LoTjvf5j3Ez13g6k8mRkFo --- tests/test-server-continuation.cpp | 125 ++++++++++++----------- tools/server/server-context.cpp | 80 ++++++++++++--- tools/server/server-continuation.h | 158 +++++++++++------------------ 3 files changed, 195 insertions(+), 168 deletions(-) diff --git a/tests/test-server-continuation.cpp b/tests/test-server-continuation.cpp index eb7650c9dd82..1b078e4f6a8c 100644 --- a/tests/test-server-continuation.cpp +++ b/tests/test-server-continuation.cpp @@ -1,6 +1,6 @@ -// A persona's next request continues the tokens her slot holds (tools/server/server-continuation.h). -// Synthetic tokens: 1 = <|im_start|>user, 2 = <|im_start|>assistant, 9 = <|im_end|> (end of turn), -// 3 = "\n", 7 = her generation prompt's , 40.. = words. +// A persona's next request carries only what is new, and continues the tokens her slot holds +// (tools/server/server-continuation.h). Synthetic tokens: 1 = <|im_start|>user, +// 2 = <|im_start|>assistant, 9 = <|im_end|> (end of turn), 3 = "\n", 7 = , 40.. = words. #include "server-continuation.h" @@ -23,74 +23,85 @@ static common_chat_msg_spans spans_of(const llama_tokens & tokens) { return spans; } -static continuation run(const llama_tokens & held, const std::vector & responses, - const std::string & continues, const llama_tokens & sent) { - const auto spans = spans_of(sent); - return continue_from({ held, responses }, { continues, sent, spans }, is_eot); +// The server's rendering of a continuing request: the stub (a user line, an assistant turn), then +// the request's new messages and the generation prompt; its delta starts at the stub's end of turn. +static llama_tokens delta_of(const llama_tokens & new_messages) { + llama_tokens rendered = { 1, 30, 9, 3, 2, 31, 9, 3 }; + rendered.insert(rendered.end(), new_messages.begin(), new_messages.end()); + rendered.insert(rendered.end(), { 2, 3, 7 }); + const auto d = delta_after_stub(rendered, spans_of(rendered), is_eot); + if (d.miss != continuation_miss::none) { + fprintf(stderr, "delta missed: %s\n", continuation_miss_name(d.miss)); + exit(1); + } + return llama_tokens(rendered.begin() + d.from, rendered.end()); } #define CHECK(cond) do { if (!(cond)) { fprintf(stderr, "%s:%d: %s\n", __FILE__, __LINE__, #cond); exit(1); } } while (0) int main() { - // The prompt she was served: a user turn, then her generation prompt. - const llama_tokens served = { 1, 40, 41, 9, 3, 2, 3, 7 }; - // What she generated after it: her reasoning (50, 51), her words (60), her call (61). No end of turn. - llama_tokens held = served; - held.insert(held.end(), { 50, 51, 60, 61 }); - const std::vector act = { { "act-1", served.size(), held.size() } }; - - // Her next request re-renders her response WITHOUT her reasoning (60, 61), then her result and the new turn. - const llama_tokens sent = { 1, 40, 41, 9, 3, 2, 3, 60, 61, 9, 3, 1, 70, 71, 9, 3, 2, 3, 7 }; - - // what this catches: her generated tokens (her reasoning) discarded and recomputed every act. + // what this catches: the stub itself (or anything before its end of turn) entering her tokens. { - const auto c = run(held, act, "act-1", sent); - CHECK(c.miss == continuation_miss::none); - llama_tokens expect = held; - expect.insert(expect.end(), { 9, 3, 1, 70, 71, 9, 3, 2, 3, 7 }); - CHECK(c.tokens == expect); - CHECK(c.from == 9); // the end of turn closing her response - CHECK(c.kept == held.size() && c.generated == 4); - CHECK(c.shift == (int64_t) held.size() - 9); - // A position past her response moves with the tail; one inside it is unknown; one before is kept. - CHECK(continued_position(c, 11) == 11 + c.shift); - CHECK(continued_position(c, 7) == -1); - CHECK(continued_position(c, 3) == 3); + const llama_tokens delta = delta_of({ 1, 70, 9, 3 }); + CHECK((delta == llama_tokens{ 9, 3, 1, 70, 9, 3, 2, 3, 7 })); // her turn's glue, the new turn, the generation prompt + } + // what this catches: Codex's act-3 miss. Three consecutive acts, each generating reasoning the + // caller never sends back: every act continues from her own tokens, nothing of hers is + // re-rendered or compared, and her slot after act 3 is exactly what she generated plus what was new. + { + llama_tokens slot = { 1, 40, 9, 3, 2, 3, 7 }; // act 1's prompt + std::vector lineage; + llama_tokens expect = slot; + for (llama_token act = 1; act <= 3; act++) { + const size_t served = slot.size(); + const llama_tokens generated = { (llama_token) (50 + act), (llama_token) (60 + act) }; // her reasoning, then her call + slot.insert(slot.end(), generated.begin(), generated.end()); + expect.insert(expect.end(), generated.begin(), generated.end()); + lineage.push_back({ "act-" + std::to_string(act), served, slot.size() }); + // Her next request: only her result and the room's new line. + const llama_tokens delta = delta_of({ 1, (llama_token) (70 + act), 9, 3 }); + const auto c = continue_from(slot, lineage, "act-" + std::to_string(act), delta); + CHECK(c.miss == continuation_miss::none); + CHECK(c.kept == slot.size() && c.generated == 2); + expect.insert(expect.end(), delta.begin(), delta.end()); + CHECK(c.tokens == expect); + slot = c.tokens; // served: her slot holds the continued prompt, her next generation follows it + } } - // what this catches: a warm pass during her tool (one token generated after her response's glue) - // making her real request miss, or its token leaking into her prompt. Both continue act-1: the - // slot's lineage still holds act-1, and only act-1's tokens are kept; the cache finds the glue. + // what this catches: a warm pass during her tool (one token generated after her response's + // glue) making her real request miss, or its token leaking into her prompt. { - llama_tokens warmed = held; - warmed.insert(warmed.end(), { 9, 3, 2, 3, 7, 80 }); - const std::vector lineage = { act[0], { "warm-1", warmed.size() - 1, warmed.size() } }; - const auto c = run(warmed, lineage, "act-1", sent); - CHECK(c.miss == continuation_miss::none); - CHECK(c.kept == held.size()); - llama_tokens expect = held; - expect.insert(expect.end(), { 9, 3, 1, 70, 71, 9, 3, 2, 3, 7 }); + llama_tokens slot = { 1, 40, 9, 3, 2, 3, 7, 51, 61 }; + std::vector lineage = { { "act-1", 7, slot.size() } }; + slot.insert(slot.end(), { 9, 3, 2, 3, 7, 80 }); + lineage.push_back({ "warm-1", slot.size() - 1, slot.size() }); + const llama_tokens delta = delta_of({ 1, 71, 9, 3 }); + const auto c = continue_from(slot, lineage, "act-1", delta); + CHECK(c.miss == continuation_miss::none && c.kept == 9); + llama_tokens expect = { 1, 40, 9, 3, 2, 3, 7, 51, 61 }; + expect.insert(expect.end(), delta.begin(), delta.end()); CHECK(c.tokens == expect); } - // what this catches: a request naming no response is served as sent. - CHECK(run(held, act, "", sent).miss == continuation_miss::not_requested); + const llama_tokens held = { 1, 40, 9, 3, 2, 3, 7, 51, 61 }; + const std::vector act = { { "act-1", 7, held.size() } }; + // what this catches: a request naming no response treated as a continuation. + CHECK(continue_from(held, act, "", { 9 }).miss == continuation_miss::not_requested); // what this catches: another response's tokens standing in for the one the request continues. - CHECK(run(held, act, "act-2", sent).miss == continuation_miss::other_response); + CHECK(continue_from(held, act, "act-2", { 9 }).miss == continuation_miss::other_response); // what this catches: a lineage that outlived its tokens (the slot was cut shorter than her response). - CHECK(run(served, act, "act-1", sent).miss == continuation_miss::other_response); - // what this catches: a slot that holds someone else's history (a different first turn) used as hers. + CHECK(continue_from(llama_tokens(held.begin(), held.begin() + 5), act, "act-1", { 9 }).miss == continuation_miss::other_response); + // what this catches: a response that generated nothing treated as holding her response. + CHECK(continue_from(held, { { "act-1", held.size(), held.size() } }, "act-1", { 9 }).miss == continuation_miss::not_generated); + // what this catches: a template with no assistant delimiter cut at a guessed point. { - llama_tokens theirs = held; - theirs[1] = 99; - CHECK(run(theirs, act, "act-1", sent).miss == continuation_miss::not_hers); + const llama_tokens no_assistant = { 1, 30, 9, 3, 1, 70, 9, 3 }; + CHECK(delta_after_stub(no_assistant, spans_of(no_assistant), is_eot).miss == continuation_miss::no_stub); + } + // what this catches: a stub turn with no end of turn cut at a guessed point. + { + const llama_tokens open = { 1, 30, 9, 3, 2, 31, 1, 70 }; + CHECK(delta_after_stub(open, spans_of(open), is_eot).miss == continuation_miss::no_end_of_turn); } - // what this catches: a response that generated nothing treated as holding her response. - CHECK(run(held, { { "act-1", held.size(), held.size() } }, "act-1", sent).miss == continuation_miss::not_generated); - // what this catches: a response whose served prompt ended before the request's response begins. - CHECK(run(held, { { "act-1", 3, held.size() } }, "act-1", sent).miss == continuation_miss::not_hers); - // what this catches: the generation prompt (the request's last span) taken for her response. - CHECK(run(held, act, "act-1", { 1, 40, 41, 9, 3, 2, 3, 7 }).miss == continuation_miss::no_response); - // what this catches: a template whose response carries no end of turn spliced at a guessed point. - CHECK(run(held, act, "act-1", { 1, 40, 41, 9, 3, 2, 3, 60, 61, 1, 70, 2, 3 }).miss == continuation_miss::no_end_of_turn); fprintf(stderr, "test-server-continuation: ok\n"); return 0; } diff --git a/tools/server/server-context.cpp b/tools/server/server-context.cpp index 65ea729506f1..22b6b0f83ca7 100644 --- a/tools/server/server-context.cpp +++ b/tools/server/server-context.cpp @@ -1952,26 +1952,20 @@ struct server_context_impl { if (task.tokens.has_mtmd || slot.prompt.tokens.has_mtmd) { return refuse(continuation_miss::has_media); } - const llama_tokens & held = slot.prompt.tokens.get_tokens(); - const llama_tokens & sent = task.tokens.get_tokens(); - const continuation c = continue_from( - continuation_held{ held, slot.responses }, - continuation_request{ task.params.continues, sent, task.params.message_spans }, - [this](llama_token tok) { return llama_vocab_is_eog(vocab, tok); }); + // The task's tokens are only its delta (cut at launch from the stub's rendering). + const llama_tokens & delta = task.tokens.get_tokens(); + const continuation c = continue_from(slot.prompt.tokens.get_tokens(), slot.responses, task.params.continues, delta); if (c.miss != continuation_miss::none) { return refuse(c.miss); } for (auto & span : task.params.message_spans.spans) { - if (span.pos >= c.from) { - span.pos += c.shift; - } else if (span.pos + span.len > c.from) { - span.len += c.shift; // her response's span now ends where her held tokens and the glue do - } + span.pos += c.kept; + } + if (task.params.view_end_pos >= 0) { + task.params.view_end_pos += (int32_t) c.kept; } - task.params.view_end_pos = (int32_t) continued_position(c, task.params.view_end_pos); - SLT_INF(slot, "continuing %s: %zu held tokens (%zu generated) kept, %zu new, %zu re-rendered tokens of her response not sent\n", - task.params.continues.c_str(), c.kept, c.generated, - sent.size() - c.from, c.from - c.response); + SLT_INF(slot, "continuing %s: %zu held tokens (%zu generated) kept, %zu new\n", + task.params.continues.c_str(), c.kept, c.generated, delta.size()); task.tokens = server_tokens(c.tokens, false); return true; } @@ -5055,6 +5049,29 @@ std::unique_ptr server_routes::handle_completions_impl( data.at("view_messages").dump().c_str(), refusal.c_str()); } } + if (!task.params.continues.empty()) { + const continuation_delta d = task.tokens.has_mtmd + ? continuation_delta{ continuation_miss::has_media, 0 } + : delta_after_stub(task.tokens.get_tokens(), task.params.message_spans, + [&](llama_token tok) { return llama_vocab_is_eog(ctx_server.vocab, tok); }); + if (d.miss != continuation_miss::none) { + res->error(format_error_response(string_format("continuation of %s refused (%s): her slot keeps her held tokens", + task.params.continues.c_str(), continuation_miss_name(d.miss)), + ERROR_TYPE_CONTINUATION_REFUSED)); + return res; + } + const llama_tokens & rendered = task.tokens.get_tokens(); + task.tokens = server_tokens(llama_tokens(rendered.begin() + d.from, rendered.end()), false); + // Her view end moves with the cut; one inside the stub names nothing of hers. + task.params.view_end_pos = task.params.view_end_pos >= (int32_t) d.from ? task.params.view_end_pos - (int32_t) d.from : -1; + common_chat_msg_spans kept; + for (const auto & span : task.params.message_spans.spans) { + if (span.pos >= d.from) { + kept.add(span.role, span.pos - d.from, span.len); + } + } + task.params.message_spans = kept; + } task.id_slot = json_value(data, "id_slot", -1); sse_ping_interval = task.params.sse_ping_interval; @@ -5276,6 +5293,38 @@ server_routes::server_routes(const common_params & params, server_context & ctx_ }); } +// A continuing request (`continues`) carries only the messages after her held response. They are +// rendered behind a short stub of that response, never the response itself: a user line, then an +// assistant turn whose tool calls are the ones the new tool results answer (a template may require +// a tool result to follow its call). The delta is cut at the stub's end-of-turn (server-continuation.h). +static void stub_her_response(json & body) { + if (json_value(body, "continues", std::string()).empty() || !body.contains("messages") || !body.at("messages").is_array()) { + return; + } + json calls = json::array(); + for (const auto & msg : body.at("messages")) { + if (json_value(msg, "role", std::string()) == "tool") { + calls.push_back({ { "id", json_value(msg, "tool_call_id", std::string()) }, + { "type", "function" }, + { "function", { { "name", "continue" }, { "arguments", "{}" } } } }); + } + } + json stub = json::array(); + stub.push_back({ { "role", "user" }, { "content", "." } }); + json assistant = { { "role", "assistant" }, { "content", "." } }; + if (!calls.empty()) { + assistant["tool_calls"] = calls; + } + stub.push_back(assistant); + for (const auto & msg : body.at("messages")) { + stub.push_back(msg); + } + body["messages"] = stub; + if (body.contains("view_messages") && body.at("view_messages").is_number_integer()) { + body["view_messages"] = body.at("view_messages").get() + 2; // the stub's two messages lead + } +} + static json get_res_model_info(const server_context_meta & meta) { // note: do NOT use ctx_server here, otherwise it's not possible to use this during sleep @@ -5715,6 +5764,7 @@ void server_routes::init_routes() { auto res = create_response(); std::vector files; json body = json::parse(req.body); + stub_her_response(body); json body_parsed = oaicompat_chat_params_parse( body, meta->chat_params, diff --git a/tools/server/server-continuation.h b/tools/server/server-continuation.h index 9e2c75c19e4a..b7f9d943e2ac 100644 --- a/tools/server/server-continuation.h +++ b/tools/server/server-continuation.h @@ -4,37 +4,35 @@ #include "common.h" #include "llama.h" -#include #include #include #include #include -// Continuum fork: a persona's next request continues the tokens her slot already holds, never -// re-rendered, re-tokenized or recomputed (CONTINUUM-ENGINE 4.14, "her token history is the -// engine's"). +// Continuum fork: a persona's next request continues the tokens her slot already holds, and carries +// only what is new (CONTINUUM-ENGINE 4.14, "her token history is the engine's"). // // After her act her slot holds [the prompt she was served][the tokens she generated]: her reasoning, -// her words and her call, exactly as sampled, without the end-of-turn token that stopped her. Her -// next request re-renders that response from its parsed message, without her reasoning, so its tokens -// differ from hers at her own response; a hybrid model's recurrent state cannot truncate there and -// rewinds to a checkpoint (the M5, 2026-11-17: 2,048 tokens recomputed on every act). A request that -// names the response it continues (`continues`, the caller's id for the request her last response answered) is -// instead served as her held tokens followed by the request's tokens from the end-of-turn that closes -// that response: the template's own glue, then everything new. +// her words and her call, exactly as sampled, without the end-of-turn token that stopped her. A +// request that names the request her response answered (`continues`) carries ONLY the messages after +// that response. The server renders them behind a short stub of her response (a user line, then an +// assistant turn whose tool calls the new tool results answer), never her response itself, and takes +// the tokens from the end-of-turn that closes the stub: the template's own glue that closes her turn, +// then the new messages. Her request is then her held tokens up to her response's end plus that delta. +// Nothing she holds is re-rendered, re-tokenized, compared or recomputed. // -// Anything that does not hold exactly is a typed miss, said by the caller: the request is then served -// as sent. +// Anything that does not hold exactly is a typed miss, and the request is refused with it +// (`continuation_refused`): her held tokens and lineage are kept. A cold start is the caller's own +// request that names no continuation. enum class continuation_miss { none, // continued not_requested, // the request names no response it continues - other_response, // her slot's last response is not the one the request continues - has_media, // a multimodal prompt is not spliced (checked by the caller: its tokens are not plain) - no_response, // the request holds no assistant message to continue after - not_hers, // her slot does not hold the request's history up to that response - not_generated, // her slot generated nothing after the prompt she was served - no_end_of_turn, // the response's rendering carries no end-of-turn token + other_response, // her slot's lineage holds no response by that id + has_media, // a multimodal prompt is not continued + no_stub, // the template's delimiters found no assistant turn for the stub + no_end_of_turn, // the stub's assistant turn carries no end-of-turn token + not_generated, // the response it names generated nothing }; inline const char * continuation_miss_name(continuation_miss miss) { @@ -43,15 +41,13 @@ inline const char * continuation_miss_name(continuation_miss miss) { case continuation_miss::not_requested: return "not_requested"; case continuation_miss::other_response: return "other_response"; case continuation_miss::has_media: return "has_media"; - case continuation_miss::no_response: return "no_response"; - case continuation_miss::not_hers: return "not_hers"; - case continuation_miss::not_generated: return "not_generated"; + case continuation_miss::no_stub: return "no_stub"; case continuation_miss::no_end_of_turn: return "no_end_of_turn"; + case continuation_miss::not_generated: return "not_generated"; } return "unknown"; } -// What her slot holds from her last response. // One response her slot holds: the caller's id for the request it answered, where the prompt she was // served for it ends and where her response ends in the slot's tokens. A slot holds a lineage of them: // a continuation keeps every earlier one as its prefix, any other request starts a new lineage. @@ -61,102 +57,72 @@ struct continuation_response { size_t n_end = 0; }; -struct continuation_held { - const llama_tokens & tokens; // her slot's tokens - const std::vector & responses; // the responses they hold, oldest first +// Where a continuing request's new tokens start in its rendering (the stub, then its new messages): +// at the end-of-turn closing the stub's assistant turn, which closes her held response in her tokens. +struct continuation_delta { + continuation_miss miss = continuation_miss::none; + size_t from = 0; }; -// The request as rendered and tokenized. -struct continuation_request { - const std::string & continues; // the id of the request whose response it continues, or empty - const llama_tokens & tokens; - const common_chat_msg_spans & spans; // its messages' token spans -}; +inline continuation_delta delta_after_stub(const llama_tokens & tokens, const common_chat_msg_spans & spans, + const std::function & is_end_of_turn) { + continuation_delta out; + const common_chat_msg_span * stub = nullptr; + for (const auto & span : spans.spans) { + if (span.role == COMMON_CHAT_ROLE_ASSISTANT) { + stub = &span; + break; + } + } + if (stub == nullptr) { + out.miss = continuation_miss::no_stub; + return out; + } + for (size_t i = stub->pos + stub->len; i > stub->pos; --i) { + if (is_end_of_turn(tokens[i - 1])) { + out.from = i - 1; + return out; + } + } + out.miss = continuation_miss::no_end_of_turn; + return out; +} struct continuation { - continuation_miss miss = continuation_miss::not_requested; - llama_tokens tokens; // her held tokens, then the request's from the response's end-of-turn - size_t response = 0; // where her response starts in the request's tokens + continuation_miss miss = continuation_miss::not_requested; + llama_tokens tokens; // her held tokens up to her response's end, then the delta size_t kept = 0; // her held tokens kept: the prompt she was served and her response size_t generated = 0; // of those, the tokens she generated - size_t from = 0; // where in the request's tokens the kept tail starts - int64_t shift = 0; // how far every request position at or after `from` moved }; -inline continuation continue_from(const continuation_held & held, const continuation_request & request, - const std::function & is_end_of_turn) { +// Her request: the held tokens of the response it continues, then its delta. `responses` is her slot's +// lineage, oldest first; a continuation of an earlier one (her real request after a warm pass that +// continued the same response) keeps only that response's tokens, and the prompt cache finds the rest. +inline continuation continue_from(const llama_tokens & held, const std::vector & responses, + const std::string & continues, const llama_tokens & delta) { continuation out; - if (request.continues.empty()) { + if (continues.empty()) { return out; } const continuation_response * answered = nullptr; - for (const auto & r : held.responses) { - if (r.id == request.continues) { + for (const auto & r : responses) { + if (r.id == continues) { answered = &r; } } - if (answered == nullptr || answered->n_end > held.tokens.size()) { + if (answered == nullptr || answered->n_end > held.size()) { out.miss = continuation_miss::other_response; return out; } - // Her response is the request's newest assistant message that something follows: everything after - // it is new. The request's own last span is the generation prompt for her next turn, never it. - const common_chat_msg_span * response = nullptr; - const auto & spans = request.spans.spans; - for (size_t i = 0; i + 1 < spans.size(); i++) { - if (spans[i].role == COMMON_CHAT_ROLE_ASSISTANT) { - response = &spans[i]; - } - } - if (response == nullptr) { - out.miss = continuation_miss::no_response; - return out; - } - // Her slot holds the request's history up to her response: the prompt she was served began with it. - const size_t start = response->pos; - if (answered->n_served < start || - !std::equal(request.tokens.begin(), request.tokens.begin() + start, held.tokens.begin())) { - out.miss = continuation_miss::not_hers; - return out; - } if (answered->n_end <= answered->n_served) { out.miss = continuation_miss::not_generated; return out; } - // The end-of-turn that closes her response in the request: what her generation stopped on. - size_t end_of_turn = start + response->len; - for (size_t i = start + response->len; i > start; --i) { - if (is_end_of_turn(request.tokens[i - 1])) { - end_of_turn = i - 1; - break; - } - } - if (end_of_turn == start + response->len) { - out.miss = continuation_miss::no_end_of_turn; - return out; - } - out.miss = continuation_miss::none; - out.response = start; + out.miss = continuation_miss::none; out.kept = answered->n_end; out.generated = answered->n_end - answered->n_served; - out.from = end_of_turn; - // Her tokens up to her response's end: whatever the slot holds after it (a warm pass's glue) is - // found again by the prompt cache, never trusted here. - out.shift = (int64_t) answered->n_end - (int64_t) end_of_turn; - out.tokens.reserve(answered->n_end + request.tokens.size() - end_of_turn); - out.tokens.insert(out.tokens.end(), held.tokens.begin(), held.tokens.begin() + answered->n_end); - out.tokens.insert(out.tokens.end(), request.tokens.begin() + end_of_turn, request.tokens.end()); + out.tokens.reserve(answered->n_end + delta.size()); + out.tokens.insert(out.tokens.end(), held.begin(), held.begin() + answered->n_end); + out.tokens.insert(out.tokens.end(), delta.begin(), delta.end()); return out; } - -// A request position after the splice: unchanged before the kept tail, moved by `shift` within it, -// and unknown (-1) inside her response, which her held tokens replace. -inline int64_t continued_position(const continuation & c, int64_t pos) { - if (pos < 0 || c.miss != continuation_miss::none || (size_t) pos <= c.response) { - return pos; - } - if ((size_t) pos < c.from) { - return -1; - } - return pos + c.shift; -}