fix(suggestion): keep a shown typo fix when only trailing spaces changed before accepting

The session ignores trailing whitespace but acceptance compared the raw
prefix, so typing one more space after the fix appeared made accept drop
the correction silently and leave the typo in place.
This commit is contained in:
Yun Chan 2026-10-20 16:27:40 +09:00
parent 1b39afe9ff
commit 32cf826290
3 changed files with 45 additions and 1 deletions

View file

@ -44,6 +44,7 @@ import {
import { renderRawChatPrompt, rawChatStopSequences } from '@d3ro/core/chat-template' import { renderRawChatPrompt, rawChatStopSequences } from '@d3ro/core/chat-template'
import { buildQueryTerms, splitSentences } from '@d3ro/core/personal-graph' import { buildQueryTerms, splitSentences } from '@d3ro/core/personal-graph'
import { analyzeWritingContext, findUnsupportedFacts } from '@d3ro/core/writing-context' import { analyzeWritingContext, findUnsupportedFacts } from '@d3ro/core/writing-context'
import { alignCorrectionToPrefix } from '@d3ro/core/typo-correction'
import { configGet, configSet } from './ConfigService' import { configGet, configSet } from './ConfigService'
import { getLogger } from './LoggerService' import { getLogger } from './LoggerService'
import { getLocalLLMService } from './LocalLLMService' import { getLocalLLMService } from './LocalLLMService'
@ -1920,7 +1921,7 @@ export class SuggestionService extends EventEmitter {
// 교정이 있으면 커서 앞 오타 구간을 지우고 고친 글 + 후보를 함께 넣는다. 그사이 원문 끝이 달라졌으면 // 교정이 있으면 커서 앞 오타 구간을 지우고 고친 글 + 후보를 함께 넣는다. 그사이 원문 끝이 달라졌으면
// (IME 마지막 글자 교체 등) 교정은 버리고 후보만 넣는다 — 엉뚱한 글자를 지우지 않는다. // (IME 마지막 글자 교체 등) 교정은 버리고 후보만 넣는다 — 엉뚱한 글자를 지우지 않는다.
const acceptPrefix = this._acceptPrefix() const acceptPrefix = this._acceptPrefix()
const correction = this._correction && acceptPrefix.endsWith(this._correction.from) ? this._correction : null const correction = this._correction ? alignCorrectionToPrefix(acceptPrefix, this._correction) : null
const basePrefix = correction const basePrefix = correction
? acceptPrefix.slice(0, acceptPrefix.length - correction.from.length) + correction.to ? acceptPrefix.slice(0, acceptPrefix.length - correction.from.length) + correction.to
: acceptPrefix : acceptPrefix

View file

@ -18,6 +18,7 @@ import {
latinToHangul, latinToHangul,
parseModelTypoPairs, parseModelTypoPairs,
planTypoReplacement, planTypoReplacement,
alignCorrectionToPrefix,
validateModelCorrections, validateModelCorrections,
typoRegionStart, typoRegionStart,
validateModelCorrections validateModelCorrections
@ -255,3 +256,25 @@ describe('낱자 잡음 (2026-10-17 실측 입력)', () => {
expect(planTypoReplacement(text, mergeTypoCorrections(model, detectJamoNoise(text)))?.insertText).toBe('안녕하세요.?') expect(planTypoReplacement(text, mergeTypoCorrections(model, detectJamoNoise(text)))?.insertText).toBe('안녕하세요.?')
}) })
}) })
describe('alignCorrectionToPrefix — 교정이 뜬 뒤 끝 공백만 달라져도 수락한다', () => {
const correction = { from: '그ㅓ런', to: '그런', edits: [] }
it('원문이 그대로면 교정을 그대로 돌려준다', () => {
expect(alignCorrectionToPrefix('그ㅓ런', correction)).toBe(correction)
})
it('공백을 더 쳤으면 그 공백까지 지우고 다시 붙인다', () => {
expect(alignCorrectionToPrefix('음 그ㅓ런 ', correction)).toEqual({ ...correction, from: '그ㅓ런 ', to: '그런 ' })
})
it('교정 뒤 공백을 지웠으면 지금 원문에 맞춘다', () => {
const spaced = { from: '그ㅓ런 ', to: '그런 ', edits: [] }
expect(alignCorrectionToPrefix('그ㅓ런', spaced)).toEqual({ ...spaced, from: '그ㅓ런', to: '그런' })
})
it('글자가 바뀌었거나 줄바꿈이 끼면 버린다', () => {
expect(alignCorrectionToPrefix('그ㅓ러', correction)).toBeNull()
expect(alignCorrectionToPrefix('그ㅓ런\n', correction)).toBeNull()
})
})

View file

@ -824,3 +824,23 @@ export function detectTypoRevert(currentPrefix: string, applied: { from: string;
const tokens = tokenizeWords(window).map((token) => token.text) const tokens = tokenizeWords(window).map((token) => token.text)
return tokens.includes(applied.from) && !tokens.includes(applied.to) return tokens.includes(applied.from) && !tokens.includes(applied.to)
} }
/**
* 표시된 교정을 수락 시점의 커서 앞 원문에 맞춘다. 맞출 수 없으면 null.
*
* 교정 구간(from)은 만들 때의 끝 공백을 그대로 담지만, 제안 세션은 끝 공백만 달라진 글을
* 같은 세션으로 본다. 교정이 뜬 뒤 공백을 하나 더 치거나 지우면 from 이 원문 끝과 어긋나
* 화면에 보이던 교정이 조용히 버려졌다 — 끝 공백을 빼고 비교하고, 지금의 끝 공백을 붙여 돌려준다.
*/
export function alignCorrectionToPrefix<T extends { from: string; to: string }>(
prefix: string,
correction: T
): T | null {
if (prefix.endsWith(correction.from)) return correction
const fromCore = correction.from.trimEnd()
const prefixCore = prefix.trimEnd()
const trailing = prefix.slice(prefixCore.length)
// 줄바꿈까지 지우고 다시 쓰는 건 앱마다 결과가 달라 위험하다.
if (fromCore.length === 0 || /[\r\n]/u.test(trailing) || !prefixCore.endsWith(fromCore)) return null
return { ...correction, from: fromCore + trailing, to: correction.to.trimEnd() + trailing }
}