Files
leleka/crates/serving-policy/oracle/gen_host_fixtures.cpp
T
Pavlo RatushnyiandClaude Sonnet 5.5 392037d83e feat(serving): port the operational HTTP surface of the Python server
control-plane serves /props, /slots, /v1/status, /api/health, /settings, /load, /unload,
/v1/load, /v1/unload, model aliases, CORS with the preflight ahead of authentication and the
own-page guard; replayed scenario by scenario against the real Python handlers. The worker
supervisor gains load/unload, lazy start, idle unload, reload gates (before_load, min free VRAM),
an outside stall watchdog and the reasoning-budget wrap-up (budget tracker golden-tested against
the Python Detokenizer/OutputParser loop). leleka-serving-policy adds CORS, idle, stall watchdog,
logits stride (golden vs the C++ header), request timings and alias rules.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-14 15:35:08 +02:00

114 lines
5.5 KiB
C++

// Host oracle for the serving policies that are plain C++ in the Original (no CUDA):
// * logits_selection.hpp - compiled UNMODIFIED from engine/strata/include;
// * the stall watchdog: core/progress.hpp (Progress, progress_now_ms) compiled unmodified, and the
// poll body of the watchdog thread in src/program/generate.cpp (the lines marked below) driven by an
// injected clock instead of sleep_for(1 s).
// Output: tests/fixtures/host_oracle.json (stride + watchdog scenarios). Usage: gen_host_fixtures OUT.json
#include <cstdint>
#include <cstdio>
#include <string>
#include <vector>
#include "strata/core/progress.hpp"
#include "strata/program/logits_selection.hpp"
namespace ls = strata::program::logits_selection;
static std::string esc(const std::string& s) {
std::string o;
for (char c : s) {
if (c == '"' || c == '\\') o += '\\';
o += c;
}
return o;
}
// ---- the watchdog thread of generate.cpp, with the clock and the counters passed in --------------------------
struct Dog {
int limit;
uint64_t last;
uint64_t ticks_at;
int64_t since_ms;
Dog(int limit_s, int64_t now_ms, uint64_t beats, uint64_t ticks)
: limit(limit_s), last(beats), ticks_at(ticks), since_ms(now_ms) {}
// Returns -1 healthy, otherwise the "layers served since the last finished step" the report prints.
// The statements below are generate.cpp's loop body (lines "const uint64_t b = ..." to "std::abort()").
int64_t poll(int64_t now_ms, uint64_t beats, bool busy, uint64_t ticks) {
if (limit <= 0) return -1; // `if (limit > 0) std::thread(...)`: no thread at all
const uint64_t b = beats;
if (!busy || b != last) { last = b; ticks_at = ticks; since_ms = now_ms; return -1; }
if (now_ms - since_ms < (int64_t) limit * 1000) return -1;
return (int64_t) (ticks - ticks_at); // stall_report(stderr, p.ticks.load() - ticks_at); abort
}
};
int main(int argc, char** argv) {
if (argc < 2) return 2;
std::FILE* f = std::fopen(argv[1], "w");
if (!f) return 2;
std::fprintf(f, "{\n \"source\": \"include/strata/program/logits_selection.hpp + core/progress.hpp + the watchdog thread of src/program/generate.cpp (host build)\",\n");
// ---- stride
std::fprintf(f, " \"parse\": [");
const char* texts[] = {"1", "2", "128", "9223372036854775807", "9223372036854775808", "0", "00", "007", "-1", "+1",
" 1", "1 ", "1x", "x", "", "1.5", "99999999999999999999", "12345678901234567890"};
bool first = true;
for (const char* t : texts) {
int64_t v = -777;
const bool ok = ls::parse_stride(t, v);
std::fprintf(f, "%s\n {\"text\": \"%s\", \"ok\": %s, \"value\": %lld}", first ? "" : ",", esc(t).c_str(), ok ? "true" : "false",
(long long) (ok ? v : -777));
first = false;
}
std::fprintf(f, "\n ],\n \"selected\": [");
first = true;
const int64_t totals[] = {-1, 0, 1, 2, 5, 9, 10, 11, 64, 100};
const int64_t strides[] = {-1, 0, 1, 2, 3, 4, 7, 10, 64, 1000};
for (int64_t total : totals)
for (int64_t stride : strides) {
std::fprintf(f, "%s\n {\"total\": %lld, \"stride\": %lld, \"rows\": %lld, \"kept\": [", first ? "" : ",", (long long) total,
(long long) stride, (long long) ls::row_count(total, stride));
bool kf = true;
for (int64_t p = -2; p < total + 2; ++p)
if (ls::selected(p, total, stride)) {
std::fprintf(f, "%s%lld", kf ? "" : ",", (long long) p);
kf = false;
}
std::fprintf(f, "]}");
first = false;
}
std::fprintf(f, "\n ],\n");
// ---- watchdog scenarios: [limit_s, [[dt_ms, beats, busy, ticks], ...]] -> verdicts
struct Step { int64_t at_ms; uint64_t beats; bool busy; uint64_t ticks; };
struct Scenario { int limit; std::vector<Step> steps; };
std::vector<Scenario> sc = {
{3, {{1000, 0, true, 0}, {2000, 0, true, 5}, {2999, 0, true, 9}, {3000, 0, true, 9}, {4000, 0, true, 9}}},
{2, {{1900, 1, true, 40}, {3800, 1, true, 40}, {3900, 1, true, 40}, {4000, 2, true, 41}, {5999, 2, true, 41}, {6000, 2, true, 80}}},
{2, {{1000, 0, false, 1}, {2000, 0, false, 2}, {3000, 0, false, 3}, {100000, 0, true, 100}, {101999, 0, true, 100}, {102000, 0, true, 100}}},
{0, {{1000, 0, true, 0}, {10000000, 0, true, 0}}},
{-5, {{1000, 0, true, 0}, {10000000, 0, true, 0}}},
{1, {{1000, 0, true, 3}, {2000, 0, true, 3}, {3000, 1, true, 3}, {3999, 1, true, 7}, {4000, 1, true, 7}}},
{60, {{59000, 0, true, 0}, {59999, 0, true, 0}, {60000, 0, true, 12}, {61000, 5, false, 12}, {200000, 5, true, 12}}},
};
std::fprintf(f, " \"watchdog\": [");
first = true;
for (const Scenario& s : sc) {
Dog dog(s.limit, 0, 0, 0);
std::fprintf(f, "%s\n {\"limit_s\": %d, \"steps\": [", first ? "" : ",", s.limit);
bool sf = true;
for (const Step& st : s.steps) {
const int64_t v = dog.poll(st.at_ms, st.beats, st.busy, st.ticks);
std::fprintf(f, "%s\n {\"at_ms\": %lld, \"beats\": %llu, \"busy\": %s, \"ticks\": %llu, \"layers_since\": %lld}", sf ? "" : ",",
(long long) st.at_ms, (unsigned long long) st.beats, st.busy ? "true" : "false", (unsigned long long) st.ticks,
(long long) v);
sf = false;
}
std::fprintf(f, "\n ]}");
first = false;
}
std::fprintf(f, "\n ]\n}\n");
std::fclose(f);
return 0;
}