Files
Strata/tests/core/serve_stop_bound_test.cpp
T
Niko1221 be101072ba serve: the thinking budget's hold (GEN hold=N:ID), so the wrap-up pass continues from the live session
At the thinking budget the server cut the stream, STOPped the engine and sent a second engine request: the same prompt + the tokens it kept + the wrap-up. The engine sees STOP only at a window's end and commits each window before sending it, so it often held one or more tokens past the cut; its live session was then not a prefix of the second request, which fell back to the checkpoint before the assistant turn, parked the live branch and read the whole reasoning again (lane PERF-AUDIT, R9700 0.1.59: 4 of 9 budget turns, 535 tokens, 849-1000 ms against 85-96 ms).

Engine: GEN key hold=N:ID ends the request after N emitted tokens unless token ID (</think>) came first; the commit is capped there (serve_hold_remaining() + serve_stop_bound(), tested against the emission loop in serve_stop_bound_test), DONE finish "hold", READY says "stop hold". Without the key nothing changes; an older engine skips the key.
Server: sends hold=<budget - thought>:<</think> id> (HOLD_KEY, its own key, never a client's) only to an engine that says hold in READY, has no --batch slots and is not a multi engine, and only when </think> is one token. At DONE hold it wraps up if the point is clean; if not (a character split across tokens, a tag held back) one more pass from the live session with a hold of the tokens left (at least one). The second pass then reads the last token and the wrap-up only. Tested with a windowed stub engine in serve/test_thinking_hold.py: the answer is the same with and without the hold for every budget; an engine that does not say hold gets no key; one that says it but ignores it gives the same answer.
--batch engines (BGEN slots) are a separate path: not covered here, no hold is sent to them.

Lane AGENT-TURN, P2.
2026-11-11 16:18:59 +01:00

74 lines
4.6 KiB
C++

// (asserts stay on in a Release build: the test is its own check)
#undef NDEBUG
#include "strata/core/serve_stop_bound.hpp"
#include <cassert>
#include <iostream>
#include <vector>
int main() {
int cases = 0;
// Independent reference: the existing emission loop, including its budget and EOS rules.
for (int accepted = 1; accepted <= 16; ++accepted)
for (int remaining = 1; remaining <= 18; ++remaining)
for (int eos = -1; eos < accepted; ++eos) {
std::vector<int> outputs(accepted);
for (int i = 0; i < accepted; ++i) outputs[i] = i == eos ? -1 : 100 + i;
std::vector<int> reference;
for (int i = 0; i < accepted && static_cast<int>(reference.size()) < remaining; ++i) {
reference.push_back(outputs[i]);
if (outputs[i] == -1) break;
}
const int kept = strata::core::serve_stop_bound(accepted, remaining, outputs.data(),
[](int t) { return t == -1; });
assert(kept > 0 && kept <= accepted && kept <= remaining);
assert(std::vector<int>(outputs.begin(), outputs.begin() + kept) == reference);
// Unlike the old full-window commit, no unexposed input suffix enters the cached history.
assert(kept == static_cast<int>(reference.size()));
++cases;
}
assert(strata::core::serve_stop_bound(4, 0, static_cast<const int*>(nullptr), [](int) { return false; }) == 0);
// GEN hold=N:ID: against the emission loop with the hold (stop after N emitted tokens unless ID came first; EOS and
// max_new as before), for every window shape, start, hold, max_new and place of ID / EOS in the window
int hold_cases = 0;
const int ID = 7777; // a real token id (a negative one means "no hold")
for (int accepted = 1; accepted <= 8; ++accepted)
for (int produced = 0; produced <= 6; ++produced)
for (int hold = 1; hold <= 10; ++hold)
for (int max_new = produced + 1; max_new <= 12; ++max_new)
for (int id_at = -1; id_at < accepted; ++id_at)
for (int eos = -1; eos < accepted; ++eos) {
if (eos >= 0 && eos == id_at) continue;
if (produced >= hold) continue; // a hold the request already reached has ended it
std::vector<int> outputs(accepted);
for (int i = 0; i < accepted; ++i) outputs[i] = i == eos ? -1 : i == id_at ? ID : 100 + i;
std::vector<int> reference;
bool on = true;
int64_t done = produced;
for (int i = 0; i < accepted; ++i) {
const int64_t cap = on ? std::min(max_new, hold) : max_new;
if (done >= cap) break;
reference.push_back(outputs[i]);
++done;
if (outputs[i] == ID) on = false;
if (outputs[i] == -1) break;
}
const int64_t rem = strata::core::serve_hold_remaining(accepted, produced, max_new, hold, ID,
outputs.data());
const int kept = strata::core::serve_stop_bound(accepted, rem, outputs.data(),
[](int t) { return t == -1; });
assert(kept == static_cast<int>(reference.size()));
assert(std::vector<int>(outputs.begin(), outputs.begin() + kept) == reference);
// no hold: exactly the max_new bound
assert(strata::core::serve_hold_remaining(accepted, produced, max_new, -1, ID, outputs.data()) ==
max_new - produced);
++hold_cases;
}
std::cout << "SUMMARY hold cases=" << hold_cases << " passed\n";
// on unless the variable is exactly "0" (every backend, since the bound left HIP-only)
assert(strata::core::serve_stop_bound_on(nullptr));
assert(strata::core::serve_stop_bound_on("1"));
assert(strata::core::serve_stop_bound_on(""));
assert(!strata::core::serve_stop_bound_on("0"));
std::cout << "SUMMARY stop_bound cases=" << cases << " passed\n";
}