From 32cf8262902ecb641ff35e4f66089a8d8003f854 Mon Sep 17 00:00:00 2001 From: Yun Chan Date: Tue, 20 Oct 2026 16:27:40 +0900 Subject: [PATCH] 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. --- .../src/main/services/SuggestionService.ts | 3 ++- .../core/__tests__/typo-correction.test.ts | 23 +++++++++++++++++++ packages/core/src/typo-correction.ts | 20 ++++++++++++++++ 3 files changed, 45 insertions(+), 1 deletion(-) diff --git a/apps/desktop/src/main/services/SuggestionService.ts b/apps/desktop/src/main/services/SuggestionService.ts index 5c940f7..36b6a1d 100644 --- a/apps/desktop/src/main/services/SuggestionService.ts +++ b/apps/desktop/src/main/services/SuggestionService.ts @@ -44,6 +44,7 @@ import { import { renderRawChatPrompt, rawChatStopSequences } from '@d3ro/core/chat-template' import { buildQueryTerms, splitSentences } from '@d3ro/core/personal-graph' import { analyzeWritingContext, findUnsupportedFacts } from '@d3ro/core/writing-context' +import { alignCorrectionToPrefix } from '@d3ro/core/typo-correction' import { configGet, configSet } from './ConfigService' import { getLogger } from './LoggerService' import { getLocalLLMService } from './LocalLLMService' @@ -1920,7 +1921,7 @@ export class SuggestionService extends EventEmitter { // 교정이 있으면 커서 앞 오타 구간을 지우고 고친 글 + 후보를 함께 넣는다. 그사이 원문 끝이 달라졌으면 // (IME 마지막 글자 교체 등) 교정은 버리고 후보만 넣는다 — 엉뚱한 글자를 지우지 않는다. 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 ? acceptPrefix.slice(0, acceptPrefix.length - correction.from.length) + correction.to : acceptPrefix diff --git a/packages/core/__tests__/typo-correction.test.ts b/packages/core/__tests__/typo-correction.test.ts index 6d820be..28c76e4 100644 --- a/packages/core/__tests__/typo-correction.test.ts +++ b/packages/core/__tests__/typo-correction.test.ts @@ -18,6 +18,7 @@ import { latinToHangul, parseModelTypoPairs, planTypoReplacement, + alignCorrectionToPrefix, validateModelCorrections, typoRegionStart, validateModelCorrections @@ -255,3 +256,25 @@ describe('낱자 잡음 (2026-10-17 실측 입력)', () => { 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() + }) +}) diff --git a/packages/core/src/typo-correction.ts b/packages/core/src/typo-correction.ts index ad08b11..95354c5 100644 --- a/packages/core/src/typo-correction.ts +++ b/packages/core/src/typo-correction.ts @@ -824,3 +824,23 @@ export function detectTypoRevert(currentPrefix: string, applied: { from: string; const tokens = tokenizeWords(window).map((token) => token.text) return tokens.includes(applied.from) && !tokens.includes(applied.to) } + +/** + * 표시된 교정을 수락 시점의 커서 앞 원문에 맞춘다. 맞출 수 없으면 null. + * + * 교정 구간(from)은 만들 때의 끝 공백을 그대로 담지만, 제안 세션은 끝 공백만 달라진 글을 + * 같은 세션으로 본다. 교정이 뜬 뒤 공백을 하나 더 치거나 지우면 from 이 원문 끝과 어긋나 + * 화면에 보이던 교정이 조용히 버려졌다 — 끝 공백을 빼고 비교하고, 지금의 끝 공백을 붙여 돌려준다. + */ +export function alignCorrectionToPrefix( + 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 } +}