From a28221eb6ad0f07d352a54f6665649a2704bc4f3 Mon Sep 17 00:00:00 2001 From: fl0l0u Date: Thu, 29 Oct 2026 12:12:57 +0000 Subject: [PATCH] sched: refuse a prompt id that names no vocabulary row The scored-prompt path (Request::token_scores) has always vetted its ids against the vocabulary; the prompt itself never was. Raw id arrays arrive at admission verbatim from the request body -- /v1/completions, /v1/embeddings and the generator take them -- and an id past the embedding table is gathered by the step, fails it, and a failed step is a device fault (spec 17): one request, {"prompt":[2147483647]}, ended every session on the server. The check belongs at admission, where a refusal is a 400, not mid-pass, where it is an exit. The sched fixture's model declares 512 vocabulary rows and its requests spell prompts from 1000 up; admission now refuses those, so the fixture's vocabulary moves above the ids it uses. --- core/sched/scheduler.cpp | 11 +++++++++++ tests/sched_test.cpp | 29 ++++++++++++++++++++++++++++- 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/core/sched/scheduler.cpp b/core/sched/scheduler.cpp index 75280b9b..39020139 100644 --- a/core/sched/scheduler.cpp +++ b/core/sched/scheduler.cpp @@ -263,6 +263,17 @@ int Scheduler::add_locked(std::unique_ptr r) { /* An embedding input runs in one step or not at all (Request::embed); one longer than a step * could never be admitted. */ if (r->embed && (int64_t)r->prompt.size() > cfg_.max_tok) return RAD_E_INVAL; + /* Every prompt id, tokenised or client-supplied, names a real vocabulary row: an id past the + * embedding table is refused at admission, never staged into a pass. Raw id arrays reach here + * straight from the request body, and an op failing mid-step is a device fault (spec §17) — + * the whole engine would die for one bad number. Same bound as the scored-prompt check below. + */ + for (int32_t t : r->prompt) + if (t < 0 || t >= prog_->meta.n_vocab) { + RAD_WARN("request %llu refused: prompt token id %d is outside the vocabulary [0, %lld)", + (unsigned long long)r->id, t, (long long)prog_->meta.n_vocab); + return RAD_E_INVAL; + } /* Nor could a prompt the KV pool cannot hold even alone: admission takes only what the pool * holds, so it would wait at the head of the queue, and everything behind it, forever. The * prompt is the least a request needs: its first token comes from the prefill's last row. diff --git a/tests/sched_test.cpp b/tests/sched_test.cpp index 92aee63d..93d317c7 100644 --- a/tests/sched_test.cpp +++ b/tests/sched_test.cpp @@ -11,6 +11,7 @@ #include "server/sink.h" #include +#include #include #include #include @@ -97,7 +98,9 @@ struct Fixture { g_gdn_row = KernelRow{ &g_gdn_ki, "fake", 0, 0 }; prog.meta.n_ctx_train = 4096; - prog.meta.n_vocab = 512; + /* Above the 1000 + i the requests below spell: admission refuses ids past the + * vocabulary, and these prompts must be admissible. */ + prog.meta.n_vocab = 65536; /* index 0 is the sentinel every handle table in Program carries */ prog.ops.emplace_back(); @@ -1280,6 +1283,30 @@ TEST(a_scored_request_names_real_vocabulary_rows) { CHECK(!kept->may_speculate); } +/* The prompt itself names real vocabulary rows too: raw id arrays arrive verbatim from the + * request body, and an id past the embedding table must die at admission, never staged into a + * pass — the embedding gather would fail the step, a failed step is a device fault, and a + * device fault stops the engine (spec §17). One bad number must not be a server kill. */ +TEST(a_prompt_names_real_vocabulary_rows) { + Fixture f; + f.build(/*blocks=*/20, /*states=*/64); + CHECK_OK(f.start()); + const int32_t n_vocab = (int32_t)f.prog.meta.n_vocab; + std::unique_ptr rs[5]; + const std::vector> ids = { { 1, 2, n_vocab - 1 }, { n_vocab }, { -1 }, + { 7, INT32_MAX }, { 0, 1, 2, n_vocab + 9 } }; + int rc[5] = { -1, -1, -1, -1, -1 }; + for (int i = 0; i < 5; ++i) { + rs[i] = std::make_unique(); + rs[i]->id = f.next_id++; + rs[i]->max_tokens = 1; + rs[i]->prompt = ids[(size_t)i]; + } + f.sch.add_all(rs, 5, rc); + CHECK_EQ(rc[0], RAD_OK); + for (int i = 1; i < 5; ++i) CHECK_EQ(rc[i], RAD_E_INVAL); +} + TEST(a_prefix_hit_starts_the_request_at_the_last_usable_checkpoint) { Fixture f; f.build();