fix(script-runner): #999 K-5 script-worker frames are cut at the frame cap; one oversized evaluation fails alone instead of killing the worker

This commit is contained in:
Joseph Doherty
2026-12-03 10:56:40 -05:00
parent c42eeb3c69
commit 8e91d6503d
6 changed files with 314 additions and 19 deletions
@@ -240,35 +240,130 @@ public sealed class ScriptWorkerFrameWriter
}
}
/// <summary>
/// Room left in a frame for the envelope header (version + sequence number, ≤ 2 × 11 bytes) and the body's own tag
/// and length prefix. Generous on purpose: the merge budget below only needs to be an upper bound.
/// </summary>
private const int FrameHeadroom = 64;
/// <summary>The bytes a merged batch's items and ids may add up to so the frame stays within the cap.</summary>
private int MergeBudget => Math.Max(1, MaxFrameBytes - FrameHeadroom);
/// <summary>
/// Whether <paramref name="item"/> fits a frame on its own (#999 / review K-5). A caller that builds an evaluation
/// checks this first and fails the one evaluation instead of handing the writer a frame it must refuse.
/// </summary>
/// <param name="item">The evaluation request or result.</param>
/// <returns>Whether it fits.</returns>
public bool FitsOneFrame(IMessage item) => ItemBytes(item) <= MergeBudget;
/// <summary>A repeated-message item's encoded size inside its batch: tag (1 byte) + length prefix + body.</summary>
private static int ItemBytes(IMessage item)
{
var size = item.CalculateSize();
return 1 + CodedOutputStream.ComputeLengthSize(size) + size;
}
/// <summary>A packed repeated uint64 id: at most 10 bytes; the per-list tag + length is in the headroom.</summary>
private const int IdBytes = 10;
/// <summary>
/// Builds one pass. Consecutive evaluations / results are merged — but, since #999 / review K-5, only up to the
/// frame cap: the merged batch is cut (and a new one started) before an item would push it over. Before, the merge
/// was unbounded and one oversized frame faulted the writer — the worker exited (host side: killed) with every
/// evaluation in flight. A single RESULT too large for any frame is replaced by an <c>UNSUPPORTED_TYPE</c> error for
/// that evaluation alone; an oversized single evaluation REQUEST is refused before it gets here
/// (<see cref="FitsOneFrame"/>), so reaching <see cref="Append"/> with one is still a protocol fault.
/// </summary>
private void FillPass()
{
EvalBatch? evals = null;
EvalResults? results = null;
var mergedBytes = 0;
var budget = MergeBudget;
void FlushEvals()
{
if (evals is null) return;
Append(new Envelope { Eval = evals });
evals = null;
mergedBytes = 0;
}
void FlushResults()
{
if (results is null) return;
Append(new Envelope { Results = results });
results = null;
mergedBytes = 0;
}
while (_queue.TryDequeue(out var envelope))
{
switch (envelope.BodyCase)
{
case Envelope.BodyOneofCase.Eval:
if (results is not null) { Append(new Envelope { Results = results }); results = null; }
(evals ??= new EvalBatch()).Items.AddRange(envelope.Eval.Items);
FlushResults();
foreach (var item in envelope.Eval.Items)
{
var bytes = ItemBytes(item);
if (evals is not null && mergedBytes + bytes > budget) FlushEvals();
(evals ??= new EvalBatch()).Items.Add(item);
mergedBytes += bytes;
}
break;
case Envelope.BodyOneofCase.Results:
if (evals is not null) { Append(new Envelope { Eval = evals }); evals = null; }
results ??= new EvalResults();
results.Items.AddRange(envelope.Results.Items);
results.StartedEvalIds.AddRange(envelope.Results.StartedEvalIds);
results.DeferredEvalIds.AddRange(envelope.Results.DeferredEvalIds);
results.SoftExpiredEvalIds.AddRange(envelope.Results.SoftExpiredEvalIds);
FlushEvals();
var body = envelope.Results;
// Ids first: an evaluation's "started" notice never trails its own result into a later frame.
foreach (var (id, list) in Ids(body))
{
if (results is not null && mergedBytes + IdBytes > budget) FlushResults();
results ??= new EvalResults();
list(results).Add(id);
mergedBytes += IdBytes;
}
foreach (var original in body.Items)
{
var item = original;
var bytes = ItemBytes(item);
if (bytes > budget)
{
item = new EvalResult
{
EvalId = original.EvalId,
AllocatedBytes = original.AllocatedBytes,
RunMicros = original.RunMicros,
Error = new ScriptError
{
Kind = ScriptError.Types.Kind.UnsupportedType,
Message = $"the script's result is {bytes} bytes encoded, over the {MaxFrameBytes}-byte frame cap",
},
};
bytes = ItemBytes(item);
}
if (results is not null && mergedBytes + bytes > budget) FlushResults();
(results ??= new EvalResults()).Items.Add(item);
mergedBytes += bytes;
}
break;
default:
if (evals is not null) { Append(new Envelope { Eval = evals }); evals = null; }
if (results is not null) { Append(new Envelope { Results = results }); results = null; }
FlushEvals();
FlushResults();
Append(envelope);
break;
}
}
if (evals is not null) Append(new Envelope { Eval = evals });
if (results is not null) Append(new Envelope { Results = results });
FlushEvals();
FlushResults();
}
/// <summary>The id lists of <paramref name="body"/>, each with the accessor for the same list on a merged batch.</summary>
private static IEnumerable<(ulong Id, Func<EvalResults, Google.Protobuf.Collections.RepeatedField<ulong>> List)> Ids(EvalResults body)
{
foreach (var id in body.StartedEvalIds) yield return (id, static r => r.StartedEvalIds);
foreach (var id in body.DeferredEvalIds) yield return (id, static r => r.DeferredEvalIds);
foreach (var id in body.SoftExpiredEvalIds) yield return (id, static r => r.SoftExpiredEvalIds);
}
private void Append(Envelope envelope)