From c2403b2e29c937169c295ee9df6eb328f22580b3 Mon Sep 17 00:00:00 2001 From: fl0l0u Date: Sun, 15 Nov 2026 17:22:20 +0000 Subject: [PATCH] kvstream: a stage run past the host layer says so, once The stage copies pool ranges of one layer into the mirror row, clamping each run to min(pool bytes, host_layer_stride) -- the clamp is what makes the copy memory-safe. But a silent clamp is a wrong answer given quietly: if pool and host layer ever disagree on the layout, a run naming a block past the row's end stages nothing there, attention reads stale bytes at those positions, and the text comes out fluent and wrong with no fault and no line to point at. stage_src now notices when it clamps or drops a run and warns once per process -- the hot path is not the place for a per-step print -- naming the group, the row's end and the stride. The behaviour is unchanged; what is new is that it is never silent. --- core/kvstream/stage.cpp | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/core/kvstream/stage.cpp b/core/kvstream/stage.cpp index 27344ecd..56d58734 100644 --- a/core/kvstream/stage.cpp +++ b/core/kvstream/stage.cpp @@ -6,6 +6,7 @@ #include "runtime/wtab.h" #include +#include #include namespace rad { @@ -202,8 +203,10 @@ bool stage_src(Ctx* ctx, int group, const RadKVGroupBatch* kvb, int layer, Stage if (end <= 0 || end > st->host_layer_stride) end = st->host_layer_stride; o->src.clear(); o->dst.clear(); o->len.clear(); o->bytes = 0; + bool clamped = false; auto add = [&](int64_t off, int64_t n) { - if (off >= end) return; + if (off >= end) { clamped = true; return; } + if (n > end - off) clamped = true; n = std::min(n, end - off); if (n <= 0) return; o->src.push_back((uint64_t)(uintptr_t)(row + off)); @@ -214,6 +217,19 @@ bool stage_src(Ctx* ctx, int group, const RadKVGroupBatch* kvb, int layer, Stage /* Only the step's ranges (stage.h, "Volume"); none set copies the row. */ if (b->stage_runs.empty()) add(0, end); for (const KVStageRun& r : b->stage_runs) add(r.off, r.bytes); + if (clamped) { + /* Once, not per step: a run naming a block past the mirror row means pool and host + * layer disagree on the layout, and the bytes past the clamp silently never stage -- + * attention then reads a row that is part copy and part stale. The clamp is the + * memory-safe answer; this is the promise that it is never a silent one. */ + static std::atomic said{false}; + bool expected = false; + if (said.compare_exchange_strong(expected, true)) + RAD_WARN("kvstream: group %d stage runs reach past the host layer's end (%lld of " + "%lld bytes): the stage drops what lies past it, and staged attention " + "reads stale bytes there (first of possibly many)", + group, (long long)end, (long long)st->host_layer_stride); + } return o->bytes > 0; }