Skip to content

fix(worker): declare worker-side truncation on the wire (#189) - #231

Merged
pdettori merged 2 commits into
rossoctl:mainfrom
pdettori:fix/189-worker-truncation-on-wire
Sep 8, 2026
Merged

pdettori merged 2 commits into
rossoctl:mainfrom
pdettori:fix/189-worker-truncation-on-wire

Conversation

@pdettori

@pdettori pdettori commented Sep 8, 2026

Copy link
Copy Markdown
Member

Closes #189.

The defect

The Go worker's non-streaming path dropped output past BufferCap and then sent End with the command's real exit code, so GrpcRelayTransport resolved {truncated: false, exitCode: 0} over cut output. This was the last path where the seam's truncation contract was violated rather than merely qualified — the same defect class as #181, which #188 closed on the other transports.

The equality of the two caps is what made it silent. BufferCap and DEFAULT_OUTPUT_CAP are both 8 MiB, and the harness trips on bytes > cap — strictly greater — so a worker delivering exactly the cap looks complete. One byte of difference in either direction would have exposed it.

What shipped

End gains bool truncated = 3. The worker keeps sending the command's real exit_code; the transport applies truncated ⇒ exitCode === null. That split keeps the wire honest for any client that wants the status and keeps the seam invariant in the one place that owes it (spec §8).

Inside the worker the count reaches the frame through a required Sink.Dropped method, not an optional http.Flusher-style extension. The triage note suggested optional for lower blast radius, but transport.ts argues the opposite about this exact flag — "truncated is REQUIRED, not optional. An optional field lets a fourth transport omit it and read as 'not truncated'" — and an optional Go extension reintroduces that silence one layer down. Required, a Sink that forgets it does not compile, which is how all four implementations got found.

The relay needed no change: relay.ts passes frame.end straight through.

Rejected

  • Making the caps deliberately unequal so ours always trips first. No proto change, and it does fix our client — but only clients whose cap is below BufferCap, and sandbox/v1 is a language-neutral contract for workers this repo does not own. It also turns a stated invariant into a stated offset for no gain once the flag exists.
  • Rejecting streaming: false at the relay. Narrows the surface without fixing the contract, and breaks legitimate non-streaming third-party clients — read/write are what the mode is for.

Tests

No first-party path reaches the defect (grpc-relay-transport.ts hardcodes streaming: true), so the scripted End{truncated: true} in grpc-relay-transport.test.ts is the artifact that makes it observable to a reviewer who cannot reach it from TypeScript.

  • runner_test.go — a non-streaming exec BufferCap + 1000 bytes long reports exactly 1000 dropped, delivers exactly BufferCap, and keeps exit 0. Watched failing with reported 0 dropped bytes, want 1000.
  • loop_test.go — the End frame carries truncated: true and the real exit code.
  • grpc-relay-transport.test.ts — resolves {exitCode: null, truncated: true} with the marker appended.
  • proto-contract.test.ts — truncated survives the wire alongside a real exit code, and defaults to false.

Both guard tests were checked to discriminate, not just to pass: they fail against an implementation reporting bytes seen rather than bytes lost. A first fault injection (if true in place of if kept < n) left them green — it only reported zero — which is why the injection was redone rather than trusted.

Two comments corrected

Both reasoned about the value this change alters, which is the coupling the triage doc exists to catch:

  • BufferCap's own doc claimed "the harness applies its own cap and truncation marker anyway". The cap equality made that false.
  • output-cap-coupling.test.ts now says equality pins memory parity, not detectability.

Docs

Spec §8 amended — the live seam contract, the documented exception in docs/specs/README.md — plus an append-only ADR-0024 Revisions entry. §4's proto listing is deliberately untouched: it already omits Chunk.stream, so field additions there are point-in-time by precedent.

Verification

go test -race ./... (both modules) · go vet · gofmt · make test (whole repo) · make typecheck (0 errors, 9 packages) · make lint (9/9 hooks). Every fix was watched failing first.

Assisted-By: Claude (Anthropic AI) [email protected]

The Go worker's non-streaming path dropped output past BufferCap and then sent
End with the command's real exit code, so GrpcRelayTransport resolved
{truncated: false, exitCode: 0} over cut output — the last path where the seam's
truncation contract was violated rather than merely qualified.

The equality of the two caps is what made it silent. BufferCap and
DEFAULT_OUTPUT_CAP are both 8 MiB and the harness trips on `bytes > cap`,
strictly greater, so a worker delivering exactly the cap looks complete. One byte
of difference in either direction would have exposed it.

End gains `bool truncated = 3`. The worker keeps sending the command's REAL exit
code; the transport applies `truncated ⇒ exitCode === null`, so the seam
invariant holds for callers while the wire stays honest for any client that wants
the status. Inside the worker the count reaches the frame through a REQUIRED
Sink.Dropped method rather than an optional extension, for the same reason
transport.ts makes `truncated` required: an omitted report reads as "nothing
dropped", which is this defect one layer down. Required, a Sink that forgets it
does not compile — and it caught all four implementations while writing this.

Rejected: making the caps deliberately unequal so ours always trips first. It
fixes our client but only clients whose cap is below BufferCap, and sandbox/v1 is
a language-neutral contract for workers this repo does not own. Also rejected:
rejecting `streaming: false` at the relay, which narrows the surface without
fixing the contract and breaks legitimate non-streaming clients.

No first-party path reaches the defect — grpc-relay-transport.ts hardcodes
`streaming: true` — so a scripted End{truncated: true} is what makes it
observable to a reviewer, alongside Go tests on the runner's accounting and the
session's frame. Both guard tests were checked to discriminate: they fail against
an implementation reporting bytes SEEN rather than bytes LOST.

Also corrects two comments that reasoned about the value this changes. BufferCap's
own doc claimed "the harness applies its own cap and truncation marker anyway",
which the cap equality made false, and output-cap-coupling.test.ts now says that
equality pins memory parity rather than detectability.

Spec §8 amended (the live seam contract, per docs/specs/README.md) and an
append-only ADR-0024 Revisions entry added.

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: Paolo Dettori <[email protected]>

@pdettori pdettori left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review

The proto change, generated stubs, transport branch, and docs are consistent, and the tests genuinely discriminate — both negative cases (TestNonStreamingReportsNoDropUnderTheCap, TestUndroppedOutputLeavesEndUntruncated) rule out the always-set and wrong-polarity implementations. The .pb.go rawDesc length bump checks out by hand: 0x39 → 0x57, +30 bytes for field 3.

Two gaps, both on the worker side, and both about which paths the flag reaches rather than the flag itself:

  1. must-fix — frameSink.Dropped is stream-blind, but the harness's cap is stdout-only, so stderr truncation over-reports and costs a valid exit code.
  2. suggestion — the abort path asserts truncated: false on an End frame, under reasoning that only holds for the timeout path's ExecError.

Both inline. CI is 12/12 green and the commit is signed off.

(Posted as a COMMENT review: GitHub rejects APPROVE/REQUEST_CHANGES on one's own PR. Verdict would otherwise be REQUEST_CHANGES on finding 1.)

Assisted-By: Claude Code

Comment thread remote-worker/internal/session/loop.go Outdated
// Dropped needs no lock of its own: the runner serializes every Sink call through
// one mutex, and it guarantees no call is in flight once Run has returned — which
// is the only point runOne reads the field.
func (f *frameSink) Dropped(_ pb.Stream, n int) { f.dropped += n }

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

must-fix — this discards stream, but the two sides don't agree on which stream truncated is about.

runner.go caps outBuf and errBuf separately, each at BufferCap, and reports drops per-stream (sink.Dropped(which, n-kept)). This sums both into one counter, so End.truncated means "either stream was cut".

The harness reads it as "the stdout I'm handing you was cut". grpc-relay-transport.ts deliberately excludes stderr from both the buffer and the byte count:

if (ev.chunk.stream !== Stream.STREAM_STDERR) {
  if (!truncated) { stdout.push(data); bytes += data.length; ...

so its own cap is stdout-only — and the new ev.end.truncated branch appends OUTPUT_TRUNCATED_MARKER to stdout and nulls the exit code.

Net effect on a non-streaming exec with >8 MiB of stderr and small, complete stdout: pre-PR it resolved {exitCode: 0, truncated: false} with whole stdout; post-PR it resolves {exitCode: null, truncated: true} with a truncation marker glued onto stdout that was never truncated. That discards a valid exit status, and since Pi reads the marker out of stdout, it reports cut output for an exec whose stdout is whole.

It is the mirror of the defect this PR closes — under-reporting became over-reporting — and it arrives through the same third-party reachability argument the PR uses to justify needing a wire field at all: streaming is relay-supplied and false is its proto3 default.

Narrowing it to the stream the flag is actually about is the smaller change:

func (f *frameSink) Dropped(stream pb.Stream, n int) {
	if stream == pb.Stream_STREAM_STDOUT {
		f.dropped += n
	}
}

If the intent really is "either stream", then the proto comment and spec §8 should say so and the transport has to stop nulling exitCode for a stderr-only cut — the larger of the two changes.

Nothing covers this today: TestNonStreamingReportsBytesDroppedAtBufferCap is stdout-only and recorder.Dropped discards the stream too, so a stderr case needs adding either way.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in a13ec88. Verified the whole chain before changing anything — pump(stdoutPipe, …, &outBuf) / pump(stderrPipe, …, &errBuf) are separately capped, sink.Dropped(which, n-kept) is faithful, and frameSink was the only place the stream was lost. The stderr-with-whole-stdout case resolves exactly as you describe.

One deliberate departure from the suggested patch: the predicate is != STREAM_STDERR rather than == STREAM_STDOUT. Since the point is that the two sides must agree on which stream the flag is about, mirroring the transport's own predicate byte-for-byte makes them agree on STREAM_UNSPECIFIED too — the proto documents 0 as stdout, so == STDOUT would silently drop a report from a worker that leaves the field unset.

I did not take the larger option. A cut stderr now gets no wire signal and is logged instead: giving the seam a second truncation concept for bytes it does not return buys nothing, but going entirely silent is what #189 was about, so it is at least visible in the worker's log. The proto comment, spec §8 and the ADR all say stdout now, and the ADR records the caveat.

On coverage — right that nothing covered it, and right about recorder.Dropped discarding the stream. It now keys per stream, and TestNonStreamingReportsDroppedBytesPerStream pins the runner's faithful reporting, which is the property the filter depends on. Worth flagging that it passes without a production change: it is a guard, not a RED test, and I would rather say so than imply it caught something.

Comment thread remote-worker/internal/session/loop.go Outdated
frame, cacheable = errFrame(reqID, fmt.Sprintf("timeout:%d", e.GetTimeoutS())), true
case errors.Is(err, wexec.ErrAborted):
frame = endFrame(reqID, -1)
frame = endFrame(reqID, -1, false)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion — the abort case drops the truncation count, but the comment above justifying that only covers the timeout case.

The reasoning two lines up is "an ExecError already tells the harness this exec produced no usable result" — true for ErrTimeout, which emits errFrame. This case emits an End, and emitBuffered runs before the switch in runner.go, so a non-streaming exec that hit BufferCap and was then aborted delivers exactly BufferCap bytes of Chunks followed by End{exit_code: -1, truncated: false} — cut output declared untruncated on the wire.

The seam invariant survives (-1 → exitCode: null, so truncated ⇒ exitCode === null holds and no caller is told the exec completed), which is why this is a suggestion and not a blocker. But a client reading truncated to decide whether its output is whole gets a false answer, and the marker never appears in stdout. Note the signalled case one line up does carry it — endFrame(reqID, code, sink.dropped > 0) with code < 0 — so the polarity here is inconsistent with its own neighbour.

sink is in scope: endFrame(reqID, -1, sink.dropped > 0) closes it.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in a13ec88 — endFrame(reqID, -1, sink.dropped > 0).

Confirmed the mechanism you name: emitBuffered really does run ahead of the exit-status switch in runner.go, so an aborted non-streaming exec that hit the cap sends BufferCap bytes of Chunks and then its End. TestAbortedExecStillReportsTruncation was watched failing with End.truncated = false before the change.

The part I found most useful was the observation about the neighbour: the signalled case one line up already reported honestly, so the two branches disagreed about the same question. That is a stronger argument than the seam-invariant one, because it does not depend on how any particular client reads the flag.

I also moved the timeout comment's scope inline rather than leaving it to be inferred — it now says the ExecError reasoning covers that case only. The comment overstating its own coverage is the thing that let this through, which is the third time in three sessions a comment has been the defect rather than the code.

…ut, report it on abort

Two review findings, both verified against the code before fixing, both correct.

MUST-FIX: frameSink.Dropped was stream-blind. The runner caps outBuf and errBuf
SEPARATELY, each at BufferCap, and reports per-stream; summing them made
End.truncated mean "either stream was cut". But the harness's cap is stdout-only —
grpc-relay-transport.ts excludes stderr from both its buffer and its byte count —
and the new branch appends the marker to stdout and nulls the exit code. So a
non-streaming exec with >8 MiB of stderr and small, complete stdout would have
resolved {exitCode: null, truncated: true} with a marker glued onto output that
was never cut. That is this PR's own defect mirrored: under-reporting traded for
over-reporting, and it discards a valid exit status.

The flag is now stdout-only. The predicate is `!= STDERR` rather than the
reviewer's `== STDOUT`, mirroring the transport's byte-for-byte, so both ends read
STREAM_UNSPECIFIED as stdout identically — the two sides must agree on that
reading as well as on the stream. A cut stderr gets no wire signal (a second
truncation concept for bytes the seam does not return buys nothing) but IS logged,
since going entirely silent is what rossoctl#189 was about.

SUGGESTION: the abort path asserted truncated: false, justified by a comment that
only covers the timeout path. Verified: emitBuffered runs BEFORE the exit-status
switch in runner.go, so an aborted non-streaming exec that hit BufferCap sends
exactly BufferCap bytes of Chunks and then End{-1}. The seam invariant survived
(-1 maps to null), which is why this was a suggestion — but a client reading
truncated got a false answer, and the signalled case one line up already reported
honestly, so the polarity was inconsistent with its own neighbour. Now carries the
flag, and the timeout comment says which case its reasoning covers.

Tests. Two new session tests, both watched failing with exactly the reviewer's
scenarios: TestDroppedStderrDoesNotMarkEndTruncated and
TestAbortedExecStillReportsTruncation. recorder.Dropped was discarding the stream
too, so it now keys per stream, and TestNonStreamingReportsDroppedBytesPerStream
pins the runner's per-stream reporting — the property frameSink's filter depends
on. That one passes without a production change, by design: it is a guard, not a
RED test.

Proto comment, spec §8 and the ADR entry all now say stdout rather than output,
and the ADR records the scope caveat and why there is no stderr signal.

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: Paolo Dettori <[email protected]>
@pdettori
pdettori merged commit 33e6919 into rossoctl:main Sep 8, 2026
12 checks passed
@pdettori
pdettori deleted the fix/189-worker-truncation-on-wire branch September 8, 2026 11:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Worker-side truncation is invisible to the seam's truncated flag: a capped non-streaming exec returns a real exit code over cut output

1 participant