Subtraction & Simplicity audit — PR #685 @ 826a427a
Verdict: the production diff is small (≈90 changed lines across dsml.rs, output.rs, encode.rs) and mostly justified, but it is not lean: the same "was this call truncated?" fact is encoded three times in three files, dsml.rs keeps a hand-maintained ordering that a one-line helper change makes unrepresentable, and +738 of the +871 added lines are tests in which the same five-element termination matrix and the same DSML envelope fixture are hand-copied between files, including a duplicated test name. Everything below is verified at head unless labelled main.
1. Subtraction — one fact, three carriers ("abandoned")
The PR introduces a third representation of one boolean. All three live simultaneously on the truncation path:
helios/src/server/output.rs:1707—abandoned: Option<usize>onResponseOutput, plusis_abandoned(:1752-1754) anddiscard_abandoned(:1757-1762), with a doc comment explaining a two-phase lifecycle: "The block stays present untildiscard_abandoned."helios/src/server/protocol/encode.rs:687let mut abandoned = false;/:698abandoned |= truncated;/:705-707if abandoned { self.incomplete_item = None; }helios/src/server/protocol/encode.rs:158incomplete_item: Option<usize>(pre-existing onmain:158), read initemat:340.
So the encoder asks is_abandoned (:694), remembers the answer in a local, then sets and immediately clears incomplete_item. The comment at :703-704 admits why: "The buffered response is rendered after this returns, so the marking outlives the change that set it unless the item is gone." That is a description of a leaked intermediate, not of a contract.
Concrete simplification: let the change carry the outcome. Change::End(usize) → Change::End(usize, Outcome) (Complete / Incomplete / Abandoned) is a one-variant addition to a 3-variant private enum (output.rs:1697-1701). Then:
item(i, complete)derives its status from the outcome it is already passed (:339-346), andincomplete_itemdisappears entirely (4 sites:158,218,340,696/706);- the
abandonedlocal and the|=/clear dance at687/698/705-707disappear; is_abandoned+discard_abandonedcollapse to "drop the block if the outcome isAbandoned", since the outcome is already in hand at:471.
That is ~25 lines removed and one invalid state (incomplete_item set for a block that no longer exists — exactly what :705-707 exists to paper over) made unrepresentable.
Bug the collapse exposes (worth fixing in the same edit): incomplete_item is a single Option on the Encoder, but the loop at :688 runs per choice and self.outputs is Vec<ResponseOutput> (:140, (0..choices) at :196). Clearing incomplete_item at :705-707 for choice k also clears the marking set for choice j < k, and a later choice's item(i, …) renders status completed for the earlier choice's truncated block. The field being per-encoder and per-choice simultaneously is the invalid state; making the outcome travel with the change removes it.
2. Simplicity — publish should own check_size
helios/src/output/dsml.rs now calls the same two functions in the same order at three sites:
:191-192call.check_size()?;/call.publish(self.calls, events);:201-203call.check_size()?;/call.publish(self.calls, events);:218-220(loop tail)if let Some(call) = &self.call { call.check_size()?; }
publish (:53-62) already mutates self and already early-returns, so the invariant "enforce the 1 MiB bound, then publish whatever is now storable" belongs inside it:
fn publish(&mut self, index: usize, events: &mut Vec<Event>) -> Result<()> {
self.check_size()?;
if self.literal.is_some() || self.published_argument_bytes == self.arguments.len() {
return Ok(());
}
...
}
Three call sites become one call each, and the ordering documented at docs/api.md:573-576 ("enforced on the append that crosses it, after the call name and earlier argument fragments were published") becomes structural rather than a comment three readers must keep in sync. This is exactly the post-publication bound the PR is asking maintainers to bless — making it a single line is what the review question at the end of the body should be resting on.
3. Simplicity — custom is computed twice to be un-combined once
helios/src/server/protocol/encode.rs:471-491:
let abandoned = self.outputs[0].is_abandoned(i);
let custom = matches!(&self.outputs[0].blocks[i], Block::Tool { name, .. } if self.tool(name).is_some_and(|tool| tool.custom));
if !abandoned && let Block::Tool { name, arguments, .. } = &self.outputs[0].blocks[i] && custom
{ /* parse + grammar */ }
…
let complete = !(abandoned && custom);
let item = self.item(i, complete);
custom is matched out of blocks[i] at :472, then matched again at :474-476 only to reach arguments, and the two booleans are recombined at :490. One match yields both, and the whole guard becomes the match arm:
match (&self.outputs[0].blocks[i], abandoned) {
(Block::Tool { name, arguments, .. }, false) if self.tool(name).is_some_and(|t| t.custom) => { /* parse + grammar */ }
_ => {}
}
The let-chains version also hides that abandoned && custom is the only combination where complete is false — with the tuple match that is visible in the arm pattern instead of in a !(a && c) expression.
4. Simplicity — blocks.last() where self.open is the answer
helios/src/server/output.rs:1737-1745:
if drops_unfinished_call(cause)
&& self.open.is_some()
&& matches!(self.blocks.last(), Some(Block::Tool { .. }))
{
let last = self.blocks.len() - 1;
changes.push(Change::End(last));
self.abandoned = Some(last);
self.open = None;
}
self.open is the index of the last block by construction (:1821 self.open = Some(i); where i == self.blocks.len(), and every close sets it to None). The blocks.last() test and the len() - 1 re-derivation are a second, weaker way to ask the same question — and they disagree in the case that matters: a Block::Tool at last that is already ended with self.open == Some(text_block). Use let Some(last) = self.open.filter(|&i| matches!(self.blocks[i], Block::Tool { .. })) and both the is_some() check and the subtraction go away.
5. Duplication in the tests — the bulk of the diff
a. The termination matrix is spelled out five times, differently each time. [OutputLimit, UserStop, ExecutionError] at dsml.rs:928-933 and :961-965; [OutputLimit, UserStop, Cancelled, ExecutionError] at protocol/tests.rs:3849-3854; [OutputLimit, UserStop, Cancelled] at :3965-3969; [OutputLimit, UserStop] at :4103. The PR body claims the tests "hold the truncation matrix as one table per property" — there is no table; there are five hand-written literals. Static const CAUSES: [Termination; 4] (or one helper for_each_truncation(f)) makes the claim true and the matrix greppable in one place.
b. The same test name exists twice. a_truncated_second_call_keeps_the_completed_first_call at dsml.rs:956 and protocol/tests.rs:3960. The dsml.rs copy drives parser + Accumulator + discard_unfinished_call by hand; the tests.rs copy drives the encoder for all three protocols. Only the encoder copy is needed once item 1 lands — the parser-level one re-tests Accumulator internals that the encoder test already covers end-to-end.
c. The DSML envelope fixture is built three different ways: dsml.rs:765-767 (envelope), protocol/tests.rs:4056 (inline literal let wire = "…"), helios/tests/deepseek_output.rs:166-175 (block). Three copies of a markup dialect that is already in helios/tests/support/.
d. for event in push { parsed.event(event) } boilerplate, four times: deepseek_output.rs:503-506, :581-582, :588-589, :593-594. A fn feed(&mut self, out: &mut OutputPipeline, text: &str) -> Result<()> on Parsed removes all four — and would have let the old assertions stay (see note below).
Note on that rewrite: the change at deepseek_output.rs:515 (assert!(published.is_empty()) → assert_eq!(parsed.ended, 0, "a rejected call reported an end")) and at :581-595 (empty-push assertions dropped) is the one place where simplification was used to weaken a test rather than to remove a line. Parsed cannot distinguish "no ToolBegin was published" from "a ToolBegin was published", so routing events through it was the only way to keep the new behaviour green. If the requirement is that a rejected call publishes nothing usable, Parsed needs a began: usize counter and the assertion should be assert_eq!((parsed.began, parsed.ended), (0, 0)). That keeps the original strength at the same line count.
6. Docs
docs/api.md:540-559 newly restates the cause→finish-reason mapping (:549-550 "a response that completes reports the termination's finish reason; only an output-limit termination reports the output limit") that the same file already states at :584-590. Two places to edit for one rule. Keep the truncation sentence at :547-552 and delete the redundant clause; also :554 has a missing wrap — …error event for streams. A client that received fragments… exceeds the paragraph width every other line in the file respects.
Explicitly not a finding
drops_unfinished_call (helios/src/server/output.rs:477-481, cause != Termination::Eos) is a one-line function, but it names a policy decision that three call sites (encode.rs:682, encode.rs, output.rs:1737) share and that the PR is asking maintainers about. That is a legitimate name, not a wrapper to inline.
Lean-ness summary
| area | added | verdict |
|---|---|---|
dsml.rs production (~25 lines) |
justified, 2 simplifications (#2) + 1 subtraction (#3-adjacent) | |
output.rs production (~35 lines) |
3 representations of one fact (#1), last() misuse (#4) |
|
encode.rs production (~45 lines) |
#1, #3 | |
tests.rs +423, dsml.rs tests +303 |
matrix duplicated 5×, one duplicate test name, fixture triplicated (#5) | |
docs/api.md +29 |
restates :584-590 (#6) |
Fixing #1 and #2 removes ~25 production lines and closes a real cross-choice status bug; folding the test matrices removes another ~40; nothing else in the diff is speculative generality or dead code.