Reuse long chat history when the last user message is edited

This commit is contained in:
Moor Wu authored and Niko1221 committed 2026-10-05 19:57:35 +02:00
1 parent 9c139846d5
commit b5de344b73
5 files changed
+138 -3

No files matched your search

+3
View File
@@ -957,6 +957,9 @@ if(STRATA_BUILD_TESTS)
add_test(NAME draft_policy_test COMMAND draft_policy_test)
endif()
if(STRATA_BUILD_TESTS OR STRATA_BUILD_CONVERSATION_TESTS)
add_executable(message_boundary_test src/program/message_boundary_test.cpp)
target_include_directories(message_boundary_test PRIVATE include)
add_test(NAME message_boundary_test COMMAND message_boundary_test)
# the serve loop's conversation-cache retention policy (header-only, no library to link)
add_executable(conv_cache_test src/program/conv_cache_test.cpp)
target_include_directories(conv_cache_test PRIVATE include)
+66
View File
@@ -0,0 +1,66 @@
# Reuse long chat history when the last user message is edited
## Why this helps
An agent can keep the same long history but replace the final user message.
The final cached state no longer matches, and the nearest periodic checkpoint
may be far behind. The engine then rereads history it already processed.
This opt-in change saves one checkpoint at the previous turn boundary, before
the final short message. It uses the existing bounded cache and leaves normal
token and image-prefix validation in charge of reuse.
## Use and limits
Set `STRATA_CACHE_MESSAGE_BOUNDARY=1` before starting the engine.
It is off by default, requires prompt caching and a valid turn token, and
only selects prefixes of at least 8,192 tokens with a tail of at most 1,024.
Layer-split execution is excluded (`multi_gpu`); helper-expert GPUs still work.
It can split prefill chunks and add snapshot cost to a fresh request.
## Validation environment
- Two modified RTX 3080 20 GB cards; CUDA SM86, CUDA 13.1, Linux.
- Intel Xeon E5-2686 v4 (18 cores / 36 threads), about 94 GiB usable RAM.
- PCIe 3.0, no GPU peer-to-peer access; 17 expert-pool workers.
- Qwen3.8-Flash-Next IQ3_S, 131,072-token context, INT8 KV,
32,768 resident KV cells.
- Medium thinking: 2,048-token reasoning cap, 8,192-token output cap.
- Physical GPU power limits stayed at 250 W and 280 W. No power increase.
- Historical performance tests used an isolated engine based on official
v0.1.38 plus reviewed architectds/Strata changes (best, 05c0f36).
This PR contains only our change, ported to official main (99f3dbd).
The historical timings are NOT a measured speedup of this pure-upstream port.
## Historical benefit
Same-binary ABCCBA comparison, two repetitions per variant:
| Metric | On-demand boundary + 16K chunks | Anticipatory boundary + 32K chunks |
| --- | ---: | ---: |
| First complete 100-record edit | 17.236 s | 11.527 s |
| Three complete edits | 41.266 s | 35.802 s |
| Cold setup plus three edits | 58.688 s | 53.146 s |
| Eleven-case wall-time sum | 133.844 s | 127.983 s |
First-edit time fell by 33.1%; reused prefix grew from 22,016 to 32,831 tokens.
This is a COMBINED boundary-policy and chunk-size comparison, not the isolated
benefit of adding this checkpoint to pristine upstream.
Hot generation was unchanged (97.55 vs 97.60 tokens/s).
The mixed-suite reduction was 4.4%; ordinary cold 8K requests cost about 0.2 s more.
The historical comparison passed 77 task/cache checks and cancellation/recovery.
A separate safety arm for the adopted 32K choice passed 22 task/cache checks,
including 120K, image-history isolation, and cancellation/recovery.
## Validation and related work
- Standalone selector test passes all 21 cases, without assertions that disappear
in Release builds.
- Linux compile/link against pure official main (99f3dbd) passed. No engine
was started and no production settings were changed.
- PR #614 checkpoints an already-reached chunk near the tail without splitting it.
This proposal chooses a message boundary, which can split chunks. They solve
related cases with different cost and numerical tradeoffs.
- Chunk geometry can change floating-point results. No pure-upstream bitwise
parity or isolated upstream speedup is claimed. Keep draft until validated.
@@ -0,0 +1,20 @@
#pragma once
#include <cstddef>
#include <cstdint>
#include <vector>
namespace strata::program {
inline int64_t message_checkpoint_boundary(const std::vector<int64_t>& ids, int64_t resume,
int64_t turn_at, int64_t turn_token, int64_t min_prefix = 8192, int64_t max_tail = 1024) {
if (resume < 0 || turn_token < 0 || turn_at <= resume || turn_at >= int64_t(ids.size()) ||
ids[std::size_t(turn_at)] != turn_token || min_prefix < 0 || max_tail <= 0) return -1;
for (int64_t i = turn_at - 1; i > resume; --i) {
if (turn_at - i > max_tail) return -1;
if (ids[std::size_t(i)] == turn_token) return i >= min_prefix ? i : -1;
}
return -1;
}
} // namespace strata::program
+16 -3
View File
@@ -56,6 +56,7 @@
#include "strata/core/native_dense.hpp"
#include "strata/program/logits_selection.hpp"
#include "strata/program/conv_cache.hpp"
#include "strata/program/message_boundary.hpp"
#include "strata/spec/draft_policy.hpp"
#include "strata/spec/draft_source.hpp"
#include "strata/spec/suffix_drafter.hpp"
@@ -8143,8 +8144,15 @@ int main(int argc, char** argv) {
if (i >= o.prompt_cache_root) root_at = i;
break;
}
static const bool message_checkpoint = [] {
const char* e = std::getenv("STRATA_CACHE_MESSAGE_BOUNDARY");
return e != nullptr && std::atoi(e) != 0;
}();
// Only add a snapshot; the existing token and image checks still decide reuse.
const int64_t message_at = message_checkpoint && !multi_gpu && o.prompt_cache > 0
? strata::program::message_checkpoint_boundary(ids, resume, turn_at, o.turn_token) : -1;
int64_t at = read_from;
for (const int64_t to : {reread_to, root_at, turn_at, n - 1}) {
for (const int64_t to : {reread_to, root_at, message_at, turn_at, n - 1}) {
if (to <= at) continue;
err.clear();
const bool win = windows_ok(at, to);
@@ -8181,10 +8189,15 @@ int main(int argc, char** argv) {
break;
}
at = to;
if ((to == turn_at || to == root_at) && !checkpoint_at(to)) {
std::printf("ERR saving a conversation checkpoint failed%s\n", ckpt_why.c_str());
if ((to == turn_at || to == root_at || to == message_at) &&
!checkpoint_at(to, nullptr, to == message_at && to != turn_at && to != root_at)) { // the tail kind leaves first
std::printf("ERR saving a conversation checkpoint failed%s
", ckpt_why.c_str());
return 1;
}
if (trace && to == message_at)
std::fprintf(stderr, "strata serve: message boundary checkpoint: %lld tokens, %lld tail\n",
(long long) message_at, (long long) (turn_at - message_at));
}
if (!refill(err)) {
std::printf("ERR refilling a lent slot failed: %s\n", err.c_str());
+33
View File
@@ -0,0 +1,33 @@
#include "strata/program/message_boundary.hpp"
#include <cstdio>
int main() {
using strata::program::message_checkpoint_boundary;
constexpr int64_t token = 248045;
std::vector<int64_t> ids(33100, 7);
ids[0] = ids[12] = ids[32800] = ids[33000] = token;
int checks = 0;
auto check = [&](int64_t resume, int64_t turn, int64_t expected) {
++checks;
return message_checkpoint_boundary(ids, resume, turn, token) == expected;
};
if (!check(0, 33000, 32800) || !check(22016, 33000, 32800) ||
!check(32800, 33000, -1) || !check(33000, 33000, -1) ||
!check(-1, 33000, -1) || !check(0, -1, -1) ||
!check(0, 33100, -1) || !check(0, 32999, -1) ||
!check(0, 12, -1) || !check(0, 32800, -1)) return 1;
for (int tail : {1, 63, 64, 512, 1024, 1025, 2048}) {
std::vector<int64_t> fixture(8192 + tail + 1, 9);
fixture[8192] = fixture[8192 + tail] = token;
if (message_checkpoint_boundary(fixture, 0, 8192 + tail, token) !=
(tail <= 1024 ? 8192 : -1)) return 1;
++checks;
}
if (message_checkpoint_boundary({}, 0, 0, token) != -1 ||
message_checkpoint_boundary(ids, 0, 33000, -1) != -1 ||
message_checkpoint_boundary(ids, 0, 33000, token, 8192, 0) != -1 ||
message_checkpoint_boundary(ids, 0, 33000, token, -1) != -1) return 1;
checks += 4;
std::printf("message_boundary_test OK (%d checks)\n", checks);
}