Preserve assistant newline semantics after #584 - #587
Conversation
|
Code-level approval on exact head Please rebase, drop the obsolete edit to the test-suite ledger deleted on current main, rerun required CI, and then mark the PR ready. The sole existing approval targets an older commit, so the refreshed exact head will need non-author review. As the PR author, I cannot self-approve formally. |
Audit disposition — product-contract decision required (2026-08-14)Hoisting Please make one explicit product decision for CRLF and the full newline-scalar set, then implement both bounded counting and preview derivation against that same contract on current main. Rebase and rerun the focused/live coverage after that decision. I would not merge semantics accidentally just because both current functions agree. |
Deep-review assessment — 2026-08-14Disposition: hold for an explicit product-level newline contract, then rebase. Hoisting Please define one intentional line contract for bounded collapse decisions and preview generation, rather than preserving an accidental Foundation component behavior. Add product-facing CRLF fixtures and rebase onto current |
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.