Preserve assistant newline semantics after #584#587
Draft
baron wants to merge 2 commits into
Draft
Conversation
provencher
approved these changes
Jul 19, 2026
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.
Summary
Character.isNewlineloop with a hoistedCharacterSet.newlinesscalar loopWhy this follows #584
This is a corrective follow-up to PR #584, not a rejection of its performance finding or a
git revert.#584 correctly identified a hot loop, but changed its iteration unit from Unicode scalars to extended-grapheme
Charactervalues. Swift exposes CRLF as one newlineCharacter, while the unchangedpreviewSummarystill usescomponents(separatedBy: .newlines), where CR and LF are separate Foundation newline separators.At the 10-line collapse boundary, merged #584 can therefore report:
That one-sided semantic change can suppress the collapsed assistant-message presentation.
The smallest correction is:
This preserves the pre-#584 scalar/CRLF behavior and realigns the decision with the existing preview contract.
Confirmed performance mechanism
Release-optimized SIL shows why the original inline form was slow: the Foundation
CharacterSet.newlinesgetter, temporary allocation, destruction, and deallocation remain inside the scalar loop. The compiler does not hoist them across the Foundation boundary. Binding the set once removes that repeated work and avoids extended-grapheme segmentation.The expert's 1M ASCII result was approximately:
Character.isNewlineTwo independent optimized reproductions confirmed the ordering:
Character.isNewlineThe hoisted scalar form also won on combining and emoji/ZWJ no-newline inputs.
Character.isNewlineretained a sub-microsecond advantage on one LF-dense early-exit shape, so this PR does not claim universal dominance; it fixes the reported long-line case while preserving the established contract.Focused coverage
AgentAssistantLineDerivationTestsprotects 26 scenarios across five methods:count/isExactexpectationspreviewSummaryThe CRLF boundary test was demonstrated red on merged #584 (
10/exactversus11/collapse) before the production correction.Validation
Passed:
make dev-test FILTER=RepoPromptTests.AgentAssistantLineDerivationTests: 5 tests, 0 failuresmake dev-lintmake dev-swift-build PRODUCT=RepoPromptKnown limitations (hence draft):
Intentionally separate follow-ups
This PR does not broaden the corrective boundary:
previewSummary's full components array with a reviewed scalar-contract single-pass derivation; exact remaining-line counts still require O(n) scanning even if retained allocation is bounded.GitService.isHexObjectIDASCII-hex strict;Character.isNumbercurrently accepts Unicode digits.