refactor(cli/purge): firm up --json and --ndjson emit semantics#287
Merged
refactor(cli/purge): firm up --json and --ndjson emit semantics#287
Conversation
Drop the allocator-backed buffer and mirror the ndjson shape: the summary now builds into a 2KB stack buffer sized for the closed schema and writes the buffered bytes in a single call, dropping any oversized payload silently. The all-or-nothing emit guarantee is structural now rather than an emergent property of how the allocating writer batches. Tests pin both the at-budget worst case and the overflow drop so a future schema bump trips a unit test instead of truncating output.
ecaafae to
c1b3a1d
Compare
Bump the per-event line buffer in the purge ndjson emitter from 512 to 1024 bytes so it matches output.emitNdjsonEvent and absorbs the same worst-case adversarial-escape names. The silent-drop fallback is now pinned by a regression test feeding an oversized scope name - "drop, not crash" is a tested invariant rather than a comment.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Two related cleanups to the purge JSON layer that tighten the emit guarantees scripts depend on. Both ride together because they touch the same file and reinforce the same wire-format contract.
1.
--jsonsummary builds into a fixed stack buffer. Previously the summary used astd.Io.Writer.Allocatingfor atomicity - all-or-nothing today, but as a side-effect of how the allocating writer batches. A future drift to incremental flushing could let a--jsonconsumer observe a half-document. The summary now mirrors the ndjson shape: build into a 2 KB stack buffer sized for the closed schema (7 scopes, longest scope name 13 chars, longesterror_kindtoken 17 chars, max u32 per-row removed counter, max u64 byte counters - worst case ~1.1 KB), write the buffered bytes in a single call, drop oversized payloads silently. Atomicity is now structural; there is no allocator dependency and no possibility of a torn document.emitSummaryno longer takes an allocator parameter, simplifying both call sites inpurge.zig.2.
--output-format=ndjsonper-event buffer aligned to 1 KB. The purge ndjson emitter used a 512-byte line buffer while the install-sideoutput.emitNdjsonEventpeer used 1024. A future schema field that pushes a purge line over 512 bytes would have hit the silent-drop branch on the closed scope set, even thoughoutput's own reasoning ("worst-case adversarial-escape name still fits in 1 KiB") leaves headroom. Both emitters now share the same ceiling, and the silent-drop fallback is pinned by a regression test feeding an oversized scope name - "drop, not crash" is a tested invariant rather than a comment.Tests pin the at-budget worst case for
--json, the overflow drop for--json, and the overflow drop for--ndjsonso future schema growth that breaks any of these budgets trips a unit test instead of truncating output.Related Issue
Notes for Reviewers