refactor(desktop): structure suggestion pipeline and restore test gates

This commit is contained in:
Yun Chan 2026-10-31 23:14:11 +09:00
parent fe0989c456
commit 1d4f8d8401
17 changed files with 813 additions and 569 deletions

View file

@ -15,7 +15,11 @@
"lint": "eslint . --ext .ts,.tsx",
"format": "prettier --write \"src/**/*.{ts,tsx,css}\"",
"test": "vitest run --config vitest.config.ts",
"test:unit": "vitest run --config vitest.config.ts",
"test:all": "vitest run --config vitest.config.ts --mode all",
"test:unit": "vitest run --config vitest.config.ts --mode unit",
"test:service": "vitest run --config vitest.config.ts --mode service",
"test:integration": "vitest run --config vitest.config.ts --mode integration",
"test:eval": "vitest run --config vitest.config.ts --mode eval",
"test:watch": "vitest --config vitest.config.ts",
"test:e2e": "playwright test",
"pack": "electron-vite build && electron-builder --dir",

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,104 @@
import type { SuggestionCandidateParseResult, SuggestionModelRequest } from './SuggestionModelRequest'
export interface FirstCandidateDiagnostics {
appName: string | null
prefix: string
composing: boolean
}
export interface FirstCandidateLogger {
debug(message: string): void
info(message: string): void
}
export interface FirstCandidateAttemptInput {
request: SuggestionModelRequest
model: string
initialTemperature: number
retryTemperature: number
signal: AbortSignal
diagnostics: FirstCandidateDiagnostics
logger: FirstCandidateLogger
stream(
request: SuggestionModelRequest,
model: string,
temperature: number,
signal: AbortSignal,
onPartial: (partial: string) => void
): Promise<string>
onPartial(partial: string): void
onRetry(): void
}
export interface FirstCandidateAttemptResult {
parsed: SuggestionCandidateParseResult
spacedPreferred: boolean
attempts: number
}
function describeRawOutput(raw: string, prefill: boolean): string {
const first = raw.charAt(0)
const kind =
raw.length === 0
? 'empty'
: first === '\n' || first === '\r'
? 'newline'
: /\s/u.test(first)
? 'space'
: /[\p{L}\p{N}]/u.test(first)
? 'letter'
: 'symbol'
return `mode=${prefill ? 'prefill' : 'instruct'} len=${raw.length} first=${kind} lines=${raw.split('\n').length}`
}
function describePrefixEnd(prefix: string): string {
if (/\s$/u.test(prefix)) return 'space'
const last = [...prefix].pop() ?? ''
if (!last) return 'empty'
if (/[\u3131-\u318e]/u.test(last)) return 'jamo'
if (/[가-힣]/u.test(last)) return 'hangul'
if (/[.!?。!?…~]/u.test(last)) return 'sentence-end'
if (/[,;:]/u.test(last)) return 'comma'
if (/[A-Za-z0-9]/u.test(last)) return 'ascii'
return 'other'
}
export async function runFirstCandidateAttempts(
input: FirstCandidateAttemptInput
): Promise<FirstCandidateAttemptResult> {
let active = input.request
let temperature = input.initialTemperature
let parsed: SuggestionCandidateParseResult = { candidate: null, rejected: 'empty' }
let attempts = 0
for (let attempt = 0; attempt < 2; attempt += 1) {
attempts = attempt + 1
const raw = await input.stream(active, input.model, temperature, input.signal, input.onPartial)
if (input.signal.aborted) break
parsed = active.parse(raw)
if (!parsed.candidate) {
input.logger.debug(`제안 정제 실패: ${describeRawOutput(raw, active.raw)}`)
if (raw.trim().length === 0) {
input.logger.debug(
`제안 빈 출력 조건: app=${input.diagnostics.appName ?? '-'} end=${describePrefixEnd(input.diagnostics.prefix)} ` +
`comp=${input.diagnostics.composing} spaced=${active !== input.request}`
)
}
}
if (parsed.candidate) break
if (raw.trim().length === 0 && active.spaced) {
input.logger.debug('제안 빈 출력 — prefill 끝에 공백을 붙여 다시 뽑는다')
input.onRetry()
active = active.spaced
} else if (parsed.rejected?.startsWith('unsupported-facts')) {
input.logger.info(`제안 후보 버림 (${parsed.rejected}) — 한 번 더 뽑는다`)
input.onRetry()
temperature = input.retryTemperature
} else {
break
}
}
return { parsed, spacedPreferred: active !== input.request, attempts }
}

View file

@ -0,0 +1,167 @@
import {
SUGGESTION_MAX_OUTPUT_CHARS,
extractContinuation,
parseSuggestionCandidates,
sanitizeSuggestionLine,
type SuggestionCandidate
} from '@d3ro/core/input-intelligence'
import { renderRawChatPrompt, rawChatStopSequences } from '@d3ro/core/chat-template'
import { splitSentences } from '@d3ro/core/personal-graph'
import { copiesContext } from '@d3ro/core/suggestion-confidence'
import { findUnsupportedFacts } from '@d3ro/core/writing-context'
import type { TypingContext } from '../InputTelemetryService'
import { buildSuggestionPrompt, type RecentWriteHint } from '../llm-prompts'
export interface LocalMemoryContext {
continuationHints: string[]
followUpHints: string[]
relatedHints: string[]
phraseHints: string[]
fallbackPhraseHints: string[]
appPhraseCount: number
}
export interface SuggestionCandidateParseResult {
candidate: SuggestionCandidate | null
rejected: string | null
}
export interface SuggestionModelRequest {
prompt: string
systemPrompt?: string
raw: boolean
stop?: string[]
parse(raw: string): SuggestionCandidateParseResult
partial(raw: string): string
/** 빈 prefill 출력일 때 끝 공백을 붙여 다시 볼 요청. */
spaced?: SuggestionModelRequest
}
export interface SuggestionModelRequestInput {
model: string
prefix: string
context: TypingContext
memory: LocalMemoryContext
maxChars: number
recentWrites?: readonly RecentWriteHint[]
avoidCandidates?: readonly string[]
}
function suffixAfterCaret(context: TypingContext): string | null {
if (context.caretOffset === null || context.caretOffset >= context.fullText.length) return null
const suffix = context.fullText.slice(context.caretOffset, context.caretOffset + 200).trim()
return suffix.length > 0 ? suffix : null
}
function relatedPromptHints(memory: LocalMemoryContext): string[] {
return [...memory.relatedHints, ...memory.phraseHints]
.filter(
(value, index, list) => list.indexOf(value) === index && !memory.followUpHints.includes(value)
)
.slice(0, 5)
}
function parsePrefillCandidate(
raw: string,
extract: (value: string) => ReturnType<typeof extractContinuation>,
factSource: string
): SuggestionCandidateParseResult {
const continuation = extract(raw)
if (!continuation) return { candidate: null, rejected: 'empty' }
const unsupported = findUnsupportedFacts(continuation.text, factSource)
if (unsupported.length > 0)
return { candidate: null, rejected: `unsupported-facts:${unsupported.join('/')}` }
if (copiesContext(continuation.text, factSource))
return { candidate: null, rejected: 'copied-context' }
return {
candidate:
continuation.joint === 'attach'
? { text: continuation.text, rank: 0, joint: 'attach' }
: { text: continuation.text, rank: 0 },
rejected: null
}
}
export function buildSuggestionModelRequest(
input: SuggestionModelRequestInput
): SuggestionModelRequest {
const { model, prefix, context, memory, maxChars, recentWrites = [], avoidCandidates } = input
const promptInput = {
prefix,
documentText: context.documentBefore ?? null,
suffix: suffixAfterCaret(context),
appName: context.appName,
windowTitle: context.windowTitle,
recentWrites,
phraseHints: relatedPromptHints(memory),
continuationHints: memory.continuationHints,
followUpHints: memory.followUpHints,
avoidCandidates,
candidates: 1,
maxChars
}
const prompt = buildSuggestionPrompt({ ...promptInput, prefill: true })
const rawPrompt = renderRawChatPrompt(model, {
system: prompt.systemPrompt,
user: prompt.text,
assistantPrefix: prompt.assistantPrefix ?? ''
})
if (rawPrompt !== null) {
const written = splitSentences(context.documentBefore ?? prefix)
const extract = (raw: string) =>
extractContinuation(raw, {
mode: prompt.writing.mode,
prefill: prompt.assistantPrefix ?? '',
writtenSentences: written,
wordFinished: prompt.wordFinished,
allowEmoji: prompt.allowEmoji,
maxChars
})
const parse = (raw: string): SuggestionCandidateParseResult =>
parsePrefillCandidate(raw, extract, prompt.factSource)
const stop = rawChatStopSequences(model)
const endsWithSpace = /\s$/u.test(prompt.assistantPrefix ?? '')
return {
prompt: rawPrompt,
raw: true,
stop,
spaced: endsWithSpace
? undefined
: {
prompt: `${rawPrompt} `,
raw: true,
stop,
parse: (raw) => parse(` ${raw}`),
partial: (raw) => extract(` ${raw}`)?.text ?? ''
},
parse,
partial: (raw) => extract(raw)?.text ?? ''
}
}
const legacy = buildSuggestionPrompt(promptInput)
return {
prompt: legacy.text,
systemPrompt: legacy.systemPrompt,
raw: false,
parse: (raw) => {
const text = parseSuggestionCandidates(raw, prefix, 1, maxChars)[0]
if (!text) return { candidate: null, rejected: 'empty' }
const unsupported = findUnsupportedFacts(text, legacy.factSource)
if (unsupported.length > 0)
return { candidate: null, rejected: `unsupported-facts:${unsupported.join('/')}` }
return { candidate: { text, rank: 0 }, rejected: null }
},
partial: sanitizePartial
}
}
function sanitizePartial(raw: string): string {
const lines = raw
.split(/\r?\n/u)
.map((line) => sanitizeSuggestionLine(line, SUGGESTION_MAX_OUTPUT_CHARS) ?? line.trim())
.filter((line) => line.length > 0)
return lines.slice(0, 2).join(' ').slice(0, SUGGESTION_MAX_OUTPUT_CHARS)
}

View file

@ -559,30 +559,30 @@ describe('입력 플로우 서비스', () => {
it('세대가 달라진 로컬 후보는 게시하지 않아 명시 취소 뒤 되살아나지 않는다', () => {
const service = getSuggestionService() as unknown as {
_generationToken: number
_publishLocalMemory: (
prefix: string,
context: Parameters<ReturnType<typeof getSuggestionService>['handleTypingContext']>[0],
maxCandidates: number,
maxChars: number,
latencyMs: number,
_publishLocalMemory: (publication: {
prefix: string
context: Parameters<ReturnType<typeof getSuggestionService>['handleTypingContext']>[0]
maxCandidates: number
maxChars: number
latencyMs: number
token: number
) => boolean
}) => boolean
}
service._generationToken = 2
expect(
service._publishLocalMemory(
'오늘 회의 결과를',
{
service._publishLocalMemory({
prefix: '오늘 회의 결과를',
context: {
prefix: '오늘 회의 결과를', fullText: '', caretOffset: null, anchor: null, anchorKind: null, isPassword: false,
isEditable: true, isComposing: false, hasSelection: false, available: true, appName: 'Notion.exe', windowTitle: null,
idleMs: 300, capturedAt: now, editedSinceFocus: true, typedRecently: true
},
3,
160,
0,
1
)
maxCandidates: 3,
maxChars: 160,
latencyMs: 0,
token: 1
})
).toBe(false)
expect(getSuggestionService().getState().visible).toBe(false)
})

View file

@ -133,8 +133,7 @@ describe('입력 중 늦게 도착하는 UIA 스냅샷', () => {
it.each([
['마우스 클릭', (value: Internal) => value._handleMouseDown({ x: 2, y: 3, button: 1 })],
['선택 드래그 끝', (value: Internal) => value._handleMouseUp({ x: 2, y: 3 })],
['스크롤', (value: Internal) => value._handleWheel({ rotation: 1 })],
['합성 유니코드 키', (value: Internal) => value._handleKeyDown({ keycode: 0 })]
['스크롤', (value: Internal) => value._handleWheel({ rotation: 1 })]
] as const)('%s 중 도착한 응답은 버린다', async (_name, change) => {
const delayed = pending()
change(internal)
@ -143,6 +142,14 @@ describe('입력 중 늦게 도착하는 UIA 스냅샷', () => {
expect(publish).not.toHaveBeenCalled()
})
it('합성 유니코드 키는 사용자 입력으로 오인하지 않고 진행 중 응답을 유지한다', async () => {
const delayed = pending()
internal._handleKeyDown({ keycode: 0 })
delayed.resolve(snapshot())
expect((await delayed.request)?.prefix).toBe('안녕하세요')
expect(publish).toHaveBeenCalledTimes(1)
})
it('통계에서 제외한 프로그램 백스페이스도 스냅샷을 무효화한다', async () => {
service.expectProgrammaticKeys(1)
const delayed = pending()

View file

@ -243,7 +243,7 @@ describe('계정별 명령·템플릿', () => {
expect(remote.find('custom_instructions', mine.id)).toBeUndefined()
expect(remote.find('user_templates', dictation.id)).toBeUndefined()
expect(remote.find('user_templates', meetingTpl.id)).toBeUndefined()
expect(getCustomInstructionService().getAll().filter((i) => i.isBuiltin)).toHaveLength(5)
expect(getCustomInstructionService().getAll().filter((i) => i.isBuiltin)).toHaveLength(6)
// A 다시 로그인: A의 항목이 돌아온다
openDb(USER)

View file

@ -387,3 +387,43 @@ describe('createElectronScreenGeometry — 좌표 공간 변환은 어댑터 한
expect(port.workAreaNear(port.cursor())).toEqual({ x: 0, y: 0, width: 1280, height: 680 })
})
})
describe('recoverPopupRenderer', () => {
function fakeWindow(): {
win: Parameters<typeof import('../../../src/main/windows/popup-window-factory').recoverPopupRenderer>[0]
reload: ReturnType<typeof vi.fn>
} {
const reload = vi.fn()
const win = {
isDestroyed: () => false,
webContents: { isDestroyed: () => false, reload },
}
return { win: win as never, reload }
}
it('크래시면 렌더러를 다시 불러온다', async () => {
const { recoverPopupRenderer } = await import('../../../src/main/windows/popup-window-factory')
const { win, reload } = fakeWindow()
expect(recoverPopupRenderer(win, 'tip', 'crashed', 1_000)).toBe(true)
expect(reload).toHaveBeenCalledTimes(1)
})
it('정상 종료·강제 종료(앱 종료 중)는 다시 불러오지 않는다', async () => {
const { recoverPopupRenderer } = await import('../../../src/main/windows/popup-window-factory')
const { win, reload } = fakeWindow()
expect(recoverPopupRenderer(win, 'tip', 'clean-exit', 1_000)).toBe(false)
expect(recoverPopupRenderer(win, 'tip', 'killed', 1_000)).toBe(false)
expect(reload).not.toHaveBeenCalled()
})
it('간격 안에 또 죽으면 다시 불러오지 않고, 간격이 지나면 다시 불러온다', async () => {
const { POPUP_RENDERER_RELOAD_INTERVAL_MS, recoverPopupRenderer } = await import(
'../../../src/main/windows/popup-window-factory'
)
const { win, reload } = fakeWindow()
recoverPopupRenderer(win, 'tip', 'oom', 1_000)
expect(recoverPopupRenderer(win, 'tip', 'oom', 1_000 + POPUP_RENDERER_RELOAD_INTERVAL_MS - 1)).toBe(false)
expect(recoverPopupRenderer(win, 'tip', 'oom', 1_000 + POPUP_RENDERER_RELOAD_INTERVAL_MS)).toBe(true)
expect(reload).toHaveBeenCalledTimes(2)
})
})

View file

@ -1,41 +0,0 @@
// tests/main/windows/popup-renderer-recovery.test.ts
// 팝업 렌더러가 죽으면 다시 불러온다(창이 살아 있어 다시 만들어지지 않으므로). 연속 크래시는 반복하지 않는다.
import { describe, expect, it, vi } from 'vitest'
import type { BrowserWindow } from 'electron'
import {
POPUP_RENDERER_RELOAD_INTERVAL_MS,
recoverPopupRenderer,
} from '../../../src/main/windows/popup-window-factory'
function fakeWindow(): { win: BrowserWindow; reload: ReturnType<typeof vi.fn> } {
const reload = vi.fn()
const win = {
isDestroyed: () => false,
webContents: { isDestroyed: () => false, reload },
} as unknown as BrowserWindow
return { win, reload }
}
describe('recoverPopupRenderer', () => {
it('크래시면 렌더러를 다시 불러온다', () => {
const { win, reload } = fakeWindow()
expect(recoverPopupRenderer(win, 'tip', 'crashed', 1_000)).toBe(true)
expect(reload).toHaveBeenCalledTimes(1)
})
it('정상 종료·강제 종료(앱 종료 중)는 다시 불러오지 않는다', () => {
const { win, reload } = fakeWindow()
expect(recoverPopupRenderer(win, 'tip', 'clean-exit', 1_000)).toBe(false)
expect(recoverPopupRenderer(win, 'tip', 'killed', 1_000)).toBe(false)
expect(reload).not.toHaveBeenCalled()
})
it('간격 안에 또 죽으면 다시 불러오지 않고, 간격이 지나면 다시 불러온다', () => {
const { win, reload } = fakeWindow()
recoverPopupRenderer(win, 'tip', 'oom', 1_000)
expect(recoverPopupRenderer(win, 'tip', 'oom', 1_000 + POPUP_RENDERER_RELOAD_INTERVAL_MS - 1)).toBe(false)
expect(recoverPopupRenderer(win, 'tip', 'oom', 1_000 + POPUP_RENDERER_RELOAD_INTERVAL_MS)).toBe(true)
expect(reload).toHaveBeenCalledTimes(2)
})
})

View file

@ -18,7 +18,8 @@ vi.mock('../../src/main/services/AudioCaptureService', () => ({
calculateRMS: () => 0,
}))
vi.mock('../../src/main/services/LocalSTTService', () => ({
vi.mock('../../src/main/services/LocalSTTService', async (importOriginal) => ({
...(await importOriginal<typeof import('../../src/main/services/LocalSTTService')>()),
getLocalSTTService: () => ({
initialize: vi.fn(async () => undefined),
transcribe: vi.fn(async () => ({

View file

@ -61,7 +61,8 @@ vi.mock('../../src/main/services/AudioCaptureService', () => ({
calculateRMS: () => 0.1,
}))
vi.mock('../../src/main/services/LocalSTTService', () => ({
vi.mock('../../src/main/services/LocalSTTService', async (importOriginal) => ({
...(await importOriginal<typeof import('../../src/main/services/LocalSTTService')>()),
getLocalSTTService: () => mockSTT,
resetLocalSTTServiceForTests: () => undefined,
}))

View file

@ -1,11 +1,20 @@
import { defineConfig } from 'vitest/config'
import { resolve } from 'path'
export default defineConfig({
const TEST_INCLUDE: Record<string, string[]> = {
all: ['tests/**/*.{test,spec}.ts'],
integration: ['tests/integration/**/*.{test,spec}.ts'],
eval: ['tests/eval/**/*.{test,spec}.ts'],
unit: ['tests/unit/**/*.{test,spec}.ts'],
service: ['tests/{main,red}/**/*.{test,spec}.ts'],
offline: ['tests/{unit,main,red}/**/*.{test,spec}.ts']
}
export default defineConfig(({ mode }) => ({
test: {
globals: true,
environment: 'node',
include: ['tests/**/*.{test,spec}.ts'],
include: TEST_INCLUDE[mode] ?? TEST_INCLUDE.offline,
exclude: ['tests/e2e/**'],
setupFiles: ['tests/setup.ts'],
testTimeout: 10000,
@ -22,4 +31,4 @@ export default defineConfig({
'@d3ro/i18n': resolve(__dirname, '../../packages/i18n/src/index.tsx')
}
}
})
}))