claude-memory-layer 1.0.11 → 1.0.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (101) hide show
  1. package/AGENTS.md +60 -0
  2. package/README.md +166 -2
  3. package/bootstrap-kb/decisions/decisions.md +244 -0
  4. package/bootstrap-kb/glossary/glossary.md +46 -0
  5. package/bootstrap-kb/modules/.claude-plugin.md +22 -0
  6. package/bootstrap-kb/modules/agents.md.md +15 -0
  7. package/bootstrap-kb/modules/claude.md.md +15 -0
  8. package/bootstrap-kb/modules/context.md.md +15 -0
  9. package/bootstrap-kb/modules/docs.md +18 -0
  10. package/bootstrap-kb/modules/handoff.md.md +15 -0
  11. package/bootstrap-kb/modules/package-lock.json.md +15 -0
  12. package/bootstrap-kb/modules/package.json.md +15 -0
  13. package/bootstrap-kb/modules/plan.md.md +15 -0
  14. package/bootstrap-kb/modules/readme.md.md +15 -0
  15. package/bootstrap-kb/modules/scripts.md +26 -0
  16. package/bootstrap-kb/modules/spec.md.md +15 -0
  17. package/bootstrap-kb/modules/specs.md +20 -0
  18. package/bootstrap-kb/modules/src.md +51 -0
  19. package/bootstrap-kb/modules/tests.md +42 -0
  20. package/bootstrap-kb/modules/tsconfig.json.md +15 -0
  21. package/bootstrap-kb/modules/vitest.config.ts.md +15 -0
  22. package/bootstrap-kb/overview/overview.md +40 -0
  23. package/bootstrap-kb/sources/manifest.json +950 -0
  24. package/bootstrap-kb/sources/manifest.md +227 -0
  25. package/bootstrap-kb/timeline/timeline.md +57 -0
  26. package/d.sh +3 -0
  27. package/deploy.sh +3 -0
  28. package/dist/cli/index.js +2389 -286
  29. package/dist/cli/index.js.map +4 -4
  30. package/dist/core/index.js +1017 -132
  31. package/dist/core/index.js.map +4 -4
  32. package/dist/hooks/post-tool-use.js +1347 -202
  33. package/dist/hooks/post-tool-use.js.map +4 -4
  34. package/dist/hooks/session-end.js +1339 -194
  35. package/dist/hooks/session-end.js.map +4 -4
  36. package/dist/hooks/session-start.js +1343 -198
  37. package/dist/hooks/session-start.js.map +4 -4
  38. package/dist/hooks/stop.js +1351 -206
  39. package/dist/hooks/stop.js.map +4 -4
  40. package/dist/hooks/user-prompt-submit.js +1347 -202
  41. package/dist/hooks/user-prompt-submit.js.map +4 -4
  42. package/dist/server/api/index.js +1436 -211
  43. package/dist/server/api/index.js.map +4 -4
  44. package/dist/server/index.js +1445 -220
  45. package/dist/server/index.js.map +4 -4
  46. package/dist/services/memory-service.js +1345 -199
  47. package/dist/services/memory-service.js.map +4 -4
  48. package/dist/ui/app.js +69 -2
  49. package/dist/ui/index.html +8 -0
  50. package/docs/MCP_MEMORY_SERVICE_COMPARATIVE_REVIEW.md +271 -0
  51. package/docs/MEMU_ADOPTION.md +40 -0
  52. package/memory/.claude-plugin/commands/2026-02-25.md +263 -0
  53. package/memory/_index.md +405 -0
  54. package/memory/default/uncategorized/2026-02-25.md +4839 -0
  55. package/memory/specs/20260207-dashboard-upgrade/2026-02-25.md +142 -0
  56. package/memory/specs/citations-system/2026-02-25.md +1121 -0
  57. package/memory/specs/endless-mode/2026-02-25.md +1392 -0
  58. package/memory/specs/entity-edge-model/2026-02-25.md +1263 -0
  59. package/memory/specs/evidence-aligner-v2/2026-02-25.md +1028 -0
  60. package/memory/specs/mcp-desktop-integration/2026-02-25.md +1334 -0
  61. package/memory/specs/post-tool-use-hook/2026-02-25.md +1164 -0
  62. package/memory/specs/private-tags/2026-02-25.md +1057 -0
  63. package/memory/specs/progressive-disclosure/2026-02-25.md +1436 -0
  64. package/memory/specs/task-entity-system/2026-02-25.md +924 -0
  65. package/memory/specs/vector-outbox-v2/2026-02-25.md +1510 -0
  66. package/memory/specs/web-viewer-ui/2026-02-25.md +1709 -0
  67. package/package.json +2 -1
  68. package/scripts/build.ts +6 -0
  69. package/scripts/bump-patch-version.sh +18 -0
  70. package/src/cli/index.ts +281 -2
  71. package/src/core/consolidated-store.ts +63 -1
  72. package/src/core/consolidation-worker.ts +115 -6
  73. package/src/core/event-store.ts +14 -0
  74. package/src/core/index.ts +1 -0
  75. package/src/core/ingest-interceptor.ts +80 -0
  76. package/src/core/markdown-mirror.ts +70 -0
  77. package/src/core/md-mirror.ts +92 -0
  78. package/src/core/mongo-sync-config.ts +165 -0
  79. package/src/core/mongo-sync-worker.ts +381 -0
  80. package/src/core/retriever.ts +540 -150
  81. package/src/core/sqlite-event-store.ts +350 -1
  82. package/src/core/tag-taxonomy.ts +51 -0
  83. package/src/core/types.ts +28 -0
  84. package/src/server/api/health.ts +53 -0
  85. package/src/server/api/index.ts +3 -1
  86. package/src/server/api/stats.ts +46 -1
  87. package/src/services/bootstrap-organizer.ts +443 -0
  88. package/src/services/codex-session-history-importer.ts +474 -0
  89. package/src/services/memory-service.ts +373 -68
  90. package/src/services/session-history-importer.ts +53 -25
  91. package/src/ui/app.js +69 -2
  92. package/src/ui/index.html +8 -0
  93. package/tests/bootstrap-organizer.test.ts +111 -0
  94. package/tests/consolidation-worker.test.ts +75 -0
  95. package/tests/ingest-interceptor.test.ts +38 -0
  96. package/tests/markdown-mirror.test.ts +85 -0
  97. package/tests/md-mirror.test.ts +50 -0
  98. package/tests/retriever-fallback-chain.test.ts +223 -0
  99. package/tests/retriever-strategy-scope.test.ts +97 -0
  100. package/tests/retriever.memu-adoption.test.ts +122 -0
  101. package/tests/sqlite-event-store-replication.test.ts +92 -0
@@ -0,0 +1,1028 @@
1
+
2
+ ## 2026-02-25T12:31:26.308Z | 0639bac9-22ca-42c0-9c40-36d77d38896f
3
+ - type: session_summary
4
+ - session: import:organized
5
+ # Evidence Aligner V2 Context
6
+
7
+ > **Version**: 2.0.0
8
+ > **Created**: 2026-01-31
9
+
10
+ ## 1. 배경
11
+
12
+ ### 1.1 LLM 오프셋 계산의 문제
13
+
14
+ LLM에게 텍스트 내 정확한 문자 위치(spanStart/spanEnd)를 계산하도록 요청하면:
15
+
16
+ ```json
17
+ // LLM 출력 (문제 있음)
18
+ {
19
+ "quote": "JSONB 제거",
20
+ "spanStart": 142,
21
+ "spanEnd": 150
22
+ }
23
+ ```
24
+
25
+ **실제 문제**:
26
+ 1. LLM은 토큰 기반으로 동작하여 문자 단위 계산이 부정확
27
+ 2. 멀티바이트 문자(한글 등)에서 오프셋 계산 오류 빈번
28
+ 3. 원문을 보지 않고 추측하는 경우 발생
29
+
30
+ ### 1.2 Quote-only 방식의 장점
31
+
32
+ ```json
33
+ // LLM 출력 (개선)
34
+ {
35
+ "messageIndex": 3,
36
+ "quote": "DuckDB의 JSONB를 JSON으로 변경"
37
+ }
38
+ ```
39
+
40
+ **장점**:
41
+ 1. LLM은 인용만 담당 (잘하는 것)
42
+ 2. 정확한 위치 계산은 시스템이 담당
43
+ 3. 검증 가능: quote가 원문에 없으면 즉시 탐지
44
+
45
+ ## 2. Memo.txt 참고 사항
46
+
47
+ ### 2.1 핵심 원칙 (섹션 2.4)
48
+
49
+ > **4. EvidenceSpan은 파이프라인이 확정**
50
+ > - LLM에게 spanStart/spanEnd 요구 금지
51
+ > - LLM은 quote만 제공 → aligner가 원문에서 찾아 span을 계산
52
+
53
+ ### 2.2 Extractor 출력 스키마 (섹션 6.1)
54
+
55
+ ```json
56
+ {
57
+ "entries":[
58
+ {
59
+ "entryId":"ent_...",
60
+ "type":"fact",
61
+ "title":"DuckDB JSONB 제거",
62
+ "evidence":[{"messageIndex":3,"quote":"content JSONB → JSON"}]
63
+ }
64
+ ]
65
+ }
66
+ ```
67
+
68
+ ### 2.3 정렬 알고리즘 (섹션 6.2)
69
+
70
+ 1. **exact substring match**
71
+ 2. **normalize(공백/개행 collapse) 후 fuzzy match (최소 0.85 이상)** — optional
72
+ 3. 실패 시 `evidenceAligned=false`로 표시하고, 해당 엔트리는 **Verified 승격 금지**
73
+
74
+ ## 3. Idris2 영감 적용
75
+
76
+ ### 3.1 Proof-Carrying Data
77
+
78
+ **Idris2 개념**:
79
+ ```idris
80
+ -- 주장과 증거가 타입 수준에서 연결
81
+ data ProvenClaim : Type where
82
+ MkClaim : (claim : String) ->
83
+ (evidence : Span) ->
84
+ (proof : InSource evidence) -> -- 증거가 원문에 있다는 증명
85
+ ProvenClaim
86
+ ```
87
+
88
+ **TypeScript 적용**:
89
+ ```typescript
90
+ // 정렬 성공한 증거만 특정 타입으로
91
+ type AlignedEvidence = {
92
+ quote: string;
93
+ span: { start: number; end: number };
94
+ matchMethod: 'exact' | 'normalized' | 'fuzzy';
95
+ confidence: number; // matchMethod에 따라 범위 제한
96
+ };
97
+
98
+ // 정렬 실패는 별도 타입
99
+ type FailedEvidence = {
100
+ quote: string;
101
+ failureReason: 'not_found' | 'below_threshold' | 'ambiguous';
102
+ };
103
+
104
+ // Union으로 구분
105
+ type Evidence = AlignedEvidence | FailedEvidence;
106
+ ```
107
+
108
+ ### 3.2 Confidence 불변식
109
+
110
+ ```typescript
111
+ // Zod refinement로 불변식 검증
112
+ const AlignedEvidenceSchema = z.object({
113
+ matchMethod: z.enum(['exact', 'normalized', 'fuzzy']),
114
+ confidence: z.number()
115
+ }).refine(data => {
116
+ switch (data.matchMethod) {
117
+ case 'exact':
118
+ return data.confidence === 1.0;
119
+ case 'normalized':
120
+ return data.confidence >= 0.95 && data.confidence < 1.0;
121
+ case 'fuzzy':
122
+ return data.confidence >= 0.85 && data.confidence < 0.95;
123
+ }
124
+ }, { message: 'Confidence must match method' });
125
+ ```
126
+
127
+ ## 4. 기존 코드와의 관계
128
+
129
+ ### 4.1 현재 evidence-aligner.ts
130
+
131
+ ```typescript
132
+ // 현재 구현 (src/core/evidence-aligner.ts)
133
+ export class EvidenceAligner {
134
+ align(claims: string[], sourceContent: string): AlignmentResult {
135
+ for (const claim of claims) {
136
+ const exactSpan = this.findExactMatch(claim, sourceContent);
137
+ if (exactSpan) {
138
+ spans.push(exactSpan);
139
+ continue;
140
+ }
141
+ missingClaims.push(claim);
142
+ }
143
+ }
144
+
145
+ private findExactMatch(claim: string, source: string): EvidenceSpan | null {
146
+ const index = source.indexOf(claim);
147
+ if (index === -1) return null;
148
+ return { start: index, end: index + claim.length, ... };
149
+ }
150
+ }
151
+ ```
152
+
153
+ ### 4.2 V2 확장 포인트
154
+
155
+ | 기존 | V2 확장 |
156
+ |-----|---------|
157
+ | claims: string[] | extractedJson with messageIndex |
158
+ | sourceContent: string | sessionMessages: string[] |
159
+ | exactMatch only | exact → normalized → fuzzy fallback |
160
+ | 반환: spans + missingClaims | 반환: AlignResult with details |
161
+
162
+ ### 4.3 하위 호환성
163
+
164
+ ```typescript
165
+ // 기존 API 유지
166
+ class EvidenceAlignerV2 extends EvidenceAligner {
167
+ // 기존 메서드 오버라이드
168
+ align(claims: string[], sourceContent: string): AlignmentResult {
169
+ // V2 로직으로 처리 후 기존 형식으로 변환
170
+ const v2Result = this.alignV2([sourceContent], {
171
+ evidence: claims.map((c, i) => ({ quote: c, messageIndex: 0 }))
172
+ });
173
+ return this.convertToV1Result(v2Result);
174
+ }
175
+
176
+ // 새 API
177
+ alignV2(sessionMessages: string[], extractedJson: ExtractedData): AlignResult;
178
+ }
179
+ ```
180
+
181
+ ## 5. 정규화 전략
182
+
183
+ ### 5.1 공백 정규화
184
+
185
+ ```typescript
186
+ function normalizeWhitespace(text: string): string {
187
+ return text
188
+ .replace(/[\t\r]/g, ' ') // 탭, CR → 공백
189
+ .replace(/\n+/g, ' ') // 개행 → 공백
190
+ .replace(/ +/g, ' ') // 연속 공백 → 단일 공백
191
+ .trim();
192
+ }
193
+ ```
194
+
195
+ ### 5.2 위치 역추적 문제
196
+
197
+ 정규화 후 매칭 시, 원본 위치를 찾아야 함:
198
+
199
+ ```typescript
200
+ // 원본: "Hello World\n\nTest"
201
+ // 정규화: "Hello World Test"
202
+ // 매칭: "World Test" at 6-16 (정규화)
203
+ // 역추적: 원본에서 "World\n\nTest" 찾기
204
+
205
+ function mapToOriginal(
206
+ original: string,
207
+ normalized: string,
208
+ normalizedStart: number,
209
+ normalizedEnd: number
210
+ ): { start: number; end: number } {
211
+ // 각 문자 위치 매핑 테이블 생성
212
+ const posMap = buildPositionMap(original, normalized);
213
+ return {
214
+ start: posMap.normalizedToOriginal.get(normalizedStart)!,
215
+ end: posMap.normalizedToOriginal.get(normalizedEnd - 1)! + 1
216
+ };
217
+ }
218
+ ```
219
+
220
+ ### 5.3 유니코드 고려
221
+
222
+ ```typescript
223
+ // 한글, 이모지 등 멀티바이트 문자 처리
224
+ function normalizeUnicode(text: string): string {
225
+ return text
226
+ .normalize('NFKC') // 유니코드 정규화
227
+ .replace(/\p{Zs}/gu, ' ') // 모든 공백 문자 → 일반 공백
228
+ .replace(/\p{Cf}/gu, ''); // 보이지 않는 포맷 문자 제거
229
+ }
230
+ ```
231
+
232
+ ## 6. Fuzzy Matching 전략
233
+
234
+ ### 6.1 Levenshtein 거리
235
+
236
+ ```typescript
237
+ function levenshteinDistance(a: string, b: string): number {
238
+ const m = a.length, n = b.length;
239
+ const dp: number[][] = Array(m + 1).fill(null).map(() => Array(n + 1).fill(0));
240
+
241
+ for (let i = 0; i <= m; i++) dp[i][0] = i;
242
+ for (let j = 0; j <= n; j++) dp[0][j] = j;
243
+
244
+ for (let i = 1; i <= m; i++) {
245
+ for (let j = 1; j <= n; j++) {
246
+ const cost = a[i - 1] === b[j - 1] ? 0 : 1;
247
+ dp[i][j] = Math.min(
248
+ dp[i - 1][j] + 1, // 삭제
249
+ dp[i][j - 1] + 1, // 삽입
250
+ dp[i - 1][j - 1] + cost // 대체
251
+ );
252
+ }
253
+ }
254
+ return dp[m][n];
255
+ }
256
+
257
+ function similarity(a: string, b: string): number {
258
+ const dist = levenshteinDistance(a, b);
259
+ const maxLen = Math.max(a.length, b.length);
260
+ return 1 - dist / maxLen;
261
+ }
262
+ ```
263
+
264
+ ### 6.2 슬라이딩 윈도우 최적화
265
+
266
+ ```typescript
267
+ function findBestMatch(
268
+ quote: string,
269
+ source: string,
270
+ threshold: number
271
+ ): { start: number; end: number; score: number } | null {
272
+ const quoteLen = quote.length;
273
+ const windowSizes = [quoteLen, quoteLen * 1.1, quoteLen * 1.2]; // 다양한 윈도우
274
+
275
+ let best: { start: number; end: number; score: number } | null = null;
276
+
277
+ for (const windowSize of windowSizes) {
278
+ const size = Math.ceil(windowSize);
279
+ for (let i = 0; i <= source.length - size; i++) {
280
+ const window = source.slice(i, i + size);
281
+ const score = similarity(quote, window);
282
+
283
+ if (score >= threshold && (!best || score > best.score)) {
284
+ best = { start: i, end: i + size, score };
285
+ }
286
+ }
287
+ }
288
+
289
+ return best;
290
+ }
291
+ ```
292
+
293
+ ## 7. 에러 케이스 처리
294
+
295
+ ### 7.1 messageIndex 범위 초과
296
+
297
+ ```typescript
298
+ function validateEvidence(evidence: ExtractedEvidence, messageCount: number): ValidationResult {
299
+ if (evidence.messageIndex >= messageCount) {
300
+ return {
301
+ valid: false,
302
+ error: `messageIndex ${evidence.messageIndex} out of range (max: ${messageCount - 1})`
303
+ };
304
+ }
305
+ return { valid: true };
306
+ }
307
+ ```
308
+
309
+ ### 7.2 빈 quote
310
+
311
+ ```typescript
312
+ if (!evidence.quote || evidence.quote.trim().length === 0) {
313
+ return {
314
+ aligned: false,
315
+ failureReason: 'empty_quote'
316
+ };
317
+ }
318
+ ```
319
+
320
+ ### 7.3 애매한 매칭 (여러 위치에서 발견)
321
+
322
+ ```typescript
323
+ function handleAmbiguousMatch(
324
+ quote: string,
325
+ source: string
326
+ ): AlignResult {
327
+ const matches = findAllMatches(quote, source);
328
+
329
+ if (matches.length > 1) {
330
+ // 첫 번째 매칭 사용, 단 애매함 표시
331
+ return {
332
+ ...matches[0],
333
+ meta: { ambiguous: true, alternativeCount: matches.length - 1 }
334
+ };
335
+ }
336
+ return matches[0];
337
+ }
338
+ ```
339
+
340
+ ## 8. 성능 고려사항
341
+
342
+ ### 8.1 캐싱
343
+
344
+ ```typescript
345
+ // 정규화 결과 캐싱 (동일 소스 반복 사용 시)
346
+ const normalizeCache = new Map<string, { normalized: string; posMap: PositionMap }>();
347
+
348
+ function getCachedNormalized(source: string): { normalized: string; posMap: PositionMap } {
349
+ if (!normalizeCache.has(source)) {
350
+ const normalized = normalize(source);
351
+ const posMap = buildPositionMap(source, normalized);
352
+ normalizeCache.set(source, { normalized, posMap });
353
+ }
354
+ return normalizeCache.get(source)!;
355
+ }
356
+ ```
357
+
358
+ ### 8.2 조기 종료
359
+
360
+ ```typescript
361
+ // Exact match 성공 시 fuzzy 시도 안 함
362
+ function align(quote: string, source: string): AlignedSpan | null {
363
+ // Step 1: Exact (가장 빠름)
364
+ const exact = exactMatch(quote, source);
365
+ if (exact) return exact;
366
+
367
+ // Step 2: Normalized (중간)
368
+ const normalized = normalizedMatch(quote, source);
369
+ if (normalized) return normalized;
370
+
371
+ // Step 3: Fuzzy (가장 느림, 필요할 때만)
372
+ return fuzzyMatch(quote, source, 0.85);
373
+ }
374
+ ```
375
+
376
+ ### 8.3 긴 텍스트 처리
377
+
378
+ ```typescript
379
+ // 매우 긴 소스의 경우 분할 처리
380
+ const CHUNK_SIZE = 10000;
381
+
382
+ function alignLongSource(quote: string, source: string): AlignedSpan | null {
383
+ if (source.length <= CHUNK_SIZE) {
384
+ return align(quote, source);
385
+ }
386
+
387
+ // 청크 단위로 검색 (오버랩 적용)
388
+ const overlap = quote.length * 2;
389
+ for (let i = 0; i < source.length; i += CHUNK_SIZE - overlap) {
390
+ const chunk = source.slice(i, i + CHUNK_SIZE);
391
+ const result = align(quote, chunk);
392
+ if (result) {
393
+ return { ...result, start: result.start + i, end: result.end + i };
394
+ }
395
+ }
396
+ return null;
397
+ }
398
+ ```
399
+
400
+ ## 9. 참고 자료
401
+
402
+ - **Memo.txt**: 섹션 6 - Evidence Align 구현 지시
403
+ - **현재 구현**: `src/core/evidence-aligner.ts`
404
+ - **타입 정의**: `src/core/types.ts` - EvidenceSpan
405
+ - **AXIOMMIND**: Principle 4 - 증거 범위 확정
406
+
407
+ ## 2026-02-25T12:31:26.316Z | 4cbc96d4-1d7b-44d6-b9b2-6ef35d5c91d2
408
+ - type: session_summary
409
+ - session: import:organized
410
+ # Evidence Aligner V2 Implementation Plan
411
+
412
+ > **Version**: 2.0.0
413
+ > **Status**: Draft
414
+ > **Created**: 2026-01-31
415
+
416
+ ## Phase 1: Extractor 수정 (P0)
417
+
418
+ ### 1.1 프롬프트 변경
419
+
420
+ **작업 항목**:
421
+ - [ ] LLM 프롬프트에서 spanStart/spanEnd 요구 제거
422
+ - [ ] quote 필드 필수로 변경
423
+ - [ ] quote 길이 가이드라인 추가 (30~200자)
424
+
425
+ **프롬프트 예시**:
426
+ ```
427
+ 각 entry에 대해 evidence를 제공하세요.
428
+ - messageIndex: 증거가 있는 메시지 인덱스 (0-based)
429
+ - quote: 원문에서 발췌한 텍스트 (30~200자)
430
+
431
+ 중요: spanStart/spanEnd는 제공하지 마세요. 시스템이 자동으로 계산합니다.
432
+ ```
433
+
434
+ ### 1.2 파서 수정
435
+
436
+ **파일**: `src/core/extractor.ts` (가정)
437
+
438
+ **작업 항목**:
439
+ - [ ] 출력 스키마에서 spanStart/spanEnd 제거
440
+ - [ ] quote 필수 검증 추가
441
+ - [ ] messageIndex 범위 검증
442
+
443
+ ## Phase 2: Aligner 핵심 구현 (P0)
444
+
445
+ ### 2.1 타입 정의
446
+
447
+ **파일**: `src/core/types.ts` 수정
448
+
449
+ ```typescript
450
+ // 추가할 타입들
451
+ export const AlignMethodSchema = z.enum(['exact', 'normalized', 'fuzzy', 'none']);
452
+
453
+ export const AlignedEvidenceSchema = z.object({
454
+ messageIndex: z.number().int().nonnegative(),
455
+ quote: z.string(),
456
+ quoteHash: z.string(),
457
+ spanStart: z.number().int().nonnegative(),
458
+ spanEnd: z.number().int().positive(),
459
+ confidence: z.number().min(0).max(1),
460
+ matchMethod: AlignMethodSchema
461
+ });
462
+ ```
463
+
464
+ **작업 항목**:
465
+ - [ ] AlignMethod 타입 추가
466
+ - [ ] AlignedEvidence 스키마 추가
467
+ - [ ] AlignResult 스키마 추가
468
+ - [ ] EvidenceAlignedEvent 타입 추가
469
+
470
+ ### 2.2 Aligner 클래스 확장
471
+
472
+ **파일**: `src/core/evidence-aligner.ts` 수정
473
+
474
+ ```typescript
475
+ export class EvidenceAlignerV2 {
476
+ constructor(private config: AlignerConfig);
477
+
478
+ // 메인 정렬 함수
479
+ async align(
480
+ sessionMessages: string[],
481
+ extractedJson: ExtractedData
482
+ ): Promise<AlignResult>;
483
+
484
+ // 단계별 매칭
485
+ private exactMatch(quote: string, source: string): AlignedSpan | null;
486
+ private normalizedMatch(quote: string, source: string): AlignedSpan | null;
487
+ private fuzzyMatch(quote: string, source: string, threshold: number): AlignedSpan | null;
488
+
489
+ // 헬퍼
490
+ private normalize(text: string): string;
491
+ private levenshteinSimilarity(a: string, b: string): number;
492
+ private mapToOriginal(source: string, normalized: string, start: number, length: number): Span;
493
+ }
494
+ ```
495
+
496
+ **작업 항목**:
497
+ - [ ] exactMatch 메서드 구현
498
+ - [ ] normalize 함수 구현
499
+ - [ ] normalizedMatch 메서드 구현 (위치 역추적 포함)
500
+ - [ ] levenshteinSimilarity 함수 구현
501
+ - [ ] fuzzyMatch 메서드 구현 (슬라이딩 윈도우)
502
+ - [ ] align 메인 함수 구현 (3단계 폴백)
503
+
504
+ ### 2.3 설정
505
+
506
+ ```typescript
507
+ interface AlignerConfig {
508
+ fuzzyThreshold: number; // default: 0.85
509
+ maxQuoteLength: number; // default: 500
510
+ enableFuzzy: boolean; // default: true
511
+ }
512
+ ```
513
+
514
+ ## Phase 3: 이벤트 연동 (P0)
515
+
516
+ ### 3.1 이벤트 발행
517
+
518
+ **파일**: `src/core/event-store.ts` 수정
519
+
520
+ **작업 항목**:
521
+ - [ ] 'evidence_aligned' 이벤트 타입 추가
522
+ - [ ] payload 스키마 정의
523
+
524
+ ### 3.2 Orchestrator 연동
525
+
526
+ **파일**: 파이프라인 연동 (graduation.ts 또는 신규)
527
+
528
+ ```typescript
529
+ async function processSession(session: Session): Promise<void> {
530
+ // 1. session_ingested 이벤트
531
+ const ingestEvent = await eventStore.append({ eventType: 'session_ingested', ... });
532
+
533
+ // 2. LLM 추출
534
+ const extracted = await extractor.extract(session);
535
+ await eventStore.append({ eventType: 'memory_extracted', ... });
536
+
537
+ // 3. Evidence 정렬 (V2)
538
+ const alignResult = await aligner.align(session.messages, extracted);
539
+ await eventStore.append({
540
+ eventType: 'evidence_aligned',
541
+ content: JSON.stringify({
542
+ source_event_id: ingestEvent.eventId,
543
+ aligned_count: alignResult.alignedEvidence.length,
544
+ failed_count: alignResult.failedQuotes.length,
545
+ evidence: alignResult.alignedEvidence,
546
+ failed_quotes: alignResult.failedQuotes
547
+ })
548
+ });
549
+
550
+ // 4. Entry 저장 (alignResult 반영)
551
+ for (const entry of extracted.entries) {
552
+ const entryEvidence = alignResult.alignedEvidence.filter(
553
+ e => e.entryId === entry.entryId
554
+ );
555
+ entry.evidenceAligned = entryEvidence.every(e => e.matchMethod !== 'none');
556
+ // ...
557
+ }
558
+ }
559
+ ```
560
+
561
+ **작업 항목**:
562
+ - [ ] process_session 흐름에 aligner 삽입
563
+ - [ ] evidence_aligned 이벤트 발행
564
+ - [ ] entry에 evidenceAligned 플래그 설정
565
+
566
+ ## Phase 4: 승격 정책 적용 (P0)
567
+
568
+ ### 4.1 Graduation 조건 수정
569
+
570
+ **파일**: `src/core/graduation.ts` 수정
571
+
572
+ ```typescript
573
+ async function promoteToVerified(entry: Entry): Promise<PromotionResult> {
574
+ // Evidence 정렬 확인
575
+ if (!entry.evidenceAligned) {
576
+ return {
577
+ success: false,
578
+ reason: 'Evidence alignment failed. Cannot promote to Verified.'
579
+ };
580
+ }
581
+
582
+ // 기타 조건 확인...
583
+ }
584
+ ```
585
+
586
+ **작업 항목**:
587
+ - [ ] candidate → verified 승격 조건에 evidenceAligned 추가
588
+ - [ ] 실패 시 명확한 reason 제공
589
+
590
+ ### 4.2 메타데이터 기록
591
+
592
+ **작업 항목**:
593
+ - [ ] entry.meta.promotionBlocked 플래그
594
+ - [ ] entry.meta.promotionBlockReason 기록
595
+ - [ ] entry.meta.failedQuotes 저장 (디버깅용)
596
+
597
+ ## Phase 5: 유틸리티 및 테스트 (P1)
598
+
599
+ ### 5.1 Levenshtein 구현
600
+
601
+ **파일**: `src/core/string-utils.ts` (신규)
602
+
603
+ ```typescript
604
+ export function levenshteinDistance(a: string, b: string): number;
605
+ export function levenshteinSimilarity(a: string, b: string): number;
606
+ export function findBestFuzzyMatch(
607
+ needle: string,
608
+ haystack: string,
609
+ threshold: number
610
+ ): { start: number; end: number; score: number } | null;
611
+ ```
612
+
613
+ **작업 항목**:
614
+ - [ ] Levenshtein 거리 함수
615
+ - [ ] 유사도 함수 (1 - distance / max_length)
616
+ - [ ] 슬라이딩 윈도우 최적 매칭
617
+
618
+ ### 5.2 위치 역추적
619
+
620
+ ```typescript
621
+ // 정규화된 문자열에서 원본 문자열 위치 매핑
622
+ interface PositionMap {
623
+ normalizedToOriginal: Map<number, number>;
624
+ originalToNormalized: Map<number, number>;
625
+ }
626
+
627
+ export function createPositionMap(original: string, normalized: string): PositionMap;
628
+ export function mapSpanToOriginal(map: PositionMap, normalizedSpan: Span): Span;
629
+ ```
630
+
631
+ **작업 항목**:
632
+ - [ ] 위치 매핑 생성 함수
633
+ - [ ] span 역추적 함수
634
+
635
+ ## 파일 목록
636
+
637
+ ### 수정 파일
638
+ ```
639
+ src/core/types.ts # 타입 추가
640
+ src/core/evidence-aligner.ts # V2 로직 추가
641
+ src/core/graduation.ts # 승격 조건 수정
642
+ src/core/event-store.ts # 이벤트 타입 추가
643
+ ```
644
+
645
+ ### 신규 파일
646
+ ```
647
+ src/core/string-utils.ts # 문자열 유틸리티
648
+ ```
649
+
650
+ ## 테스트
651
+
652
+ ### 필수 테스트 케이스
653
+
654
+ 1. **Exact Match**
655
+ ```typescript
656
+ // quote가 원문에 정확히 존재
657
+ const source = "DuckDB의 JSONB를 JSON으로 변경";
658
+ const quote = "JSONB를 JSON으로";
659
+ expect(aligner.exactMatch(quote, source)).toEqual({
660
+ spanStart: 7,
661
+ spanEnd: 18,
662
+ confidence: 1.0,
663
+ matchMethod: 'exact'
664
+ });
665
+ ```
666
+
667
+ 2. **Normalized Match**
668
+ ```typescript
669
+ // 공백 차이만 있는 경우
670
+ const source = "JSONB를 JSON으로\n변경";
671
+ const quote = "JSONB를 JSON으로 변경";
672
+ expect(aligner.normalizedMatch(quote, source)).not.toBeNull();
673
+ ```
674
+
675
+ 3. **Fuzzy Match**
676
+ ```typescript
677
+ // 약간의 오타/변형
678
+ const source = "DuckDB에서 JSONB 타입을 제거";
679
+ const quote = "DuckDB JSONB 타입 제거"; // 조사 누락
680
+ const result = aligner.fuzzyMatch(quote, source, 0.85);
681
+ expect(result?.confidence).toBeGreaterThanOrEqual(0.85);
682
+ ```
683
+
684
+ 4. **No Match**
685
+ ```typescript
686
+ // 원문에 없는 내용
687
+ const source = "벡터 검색을 구현합니다";
688
+ const quote = "JSONB를 제거";
689
+ expect(aligner.align([source], { evidence: [{ quote }] })).toEqual({
690
+ evidenceAligned: false,
691
+ failedQuotes: ["JSONB를 제거"]
692
+ });
693
+ ```
694
+
695
+ 5. **승격 거부**
696
+ ```typescript
697
+ const entry = { evidenceAligned: false, stage: 'candidate' };
698
+ const result = await graduation.promoteToVerified(entry);
699
+ expect(result.success).toBe(false);
700
+ expect(result.reason).toContain('Evidence alignment failed');
701
+ ```
702
+
703
+ ## 마일스톤
704
+
705
+ | 단계 | 완료 기준 |
706
+ |------|----------|
707
+ | M1 | 타입 정의 완료 |
708
+ | M2 | exactMatch + normalizedMatch 동작 |
709
+ | M3 | fuzzyMatch 동작 (Levenshtein) |
710
+ | M4 | evidence_aligned 이벤트 발행 |
711
+ | M5 | Graduation 승격 조건 적용 |
712
+ | M6 | 테스트 통과 |
713
+
714
+ ## 2026-02-25T12:31:26.323Z | c69c59bf-5958-4142-88c8-caa241ae54ae
715
+ - type: session_summary
716
+ - session: import:organized
717
+ # Evidence Aligner V2 Specification
718
+
719
+ > **Version**: 2.0.0
720
+ > **Status**: Draft
721
+ > **Created**: 2026-01-31
722
+
723
+ ## 1. 개요
724
+
725
+ ### 1.1 문제 정의
726
+
727
+ 현재 시스템에서 LLM이 evidence의 spanStart/spanEnd를 직접 계산하는 방식의 문제:
728
+
729
+ 1. **부정확한 오프셋**: LLM이 문자 위치를 정확히 계산하기 어려움
730
+ 2. **환각 가능성**: 원문에 없는 내용을 증거로 제시할 수 있음
731
+ 3. **검증 불가**: LLM이 준 오프셋이 실제 원문과 일치하는지 확인 어려움
732
+
733
+ ### 1.2 해결 방향
734
+
735
+ **Quote-only 방식**:
736
+ - LLM은 **quote(인용문)**만 제공
737
+ - Pipeline(EvidenceAligner)이 원문에서 정확한 span 계산
738
+ - 매칭 실패 시 `evidenceAligned=false`로 표시 → Verified 승격 금지
739
+
740
+ ## 2. 핵심 개념
741
+
742
+ ### 2.1 Evidence 흐름
743
+
744
+ ```
745
+ LLM Extractor EvidenceAligner Database
746
+ │ │ │
747
+ │ { quote: "JSONB 제거" } │ │
748
+ ├─────────────────────────────────▶│ │
749
+ │ │ 원문에서 검색 │
750
+ │ │ ───────────── │
751
+ │ │ │
752
+ │ { spanStart: 142, │ │
753
+ │ spanEnd: 150, │ │
754
+ │ confidence: 1.0 } │ │
755
+ │◀─────────────────────────────────┤ │
756
+ │ │ │
757
+ │ │ evidence_aligned 이벤트 │
758
+ │ ├─────────────────────────▶│
759
+ ```
760
+
761
+ ### 2.2 Extractor 출력 스키마 변경
762
+
763
+ **기존 (금지)**:
764
+ ```json
765
+ {
766
+ "evidence": [{
767
+ "messageIndex": 3,
768
+ "spanStart": 142,
769
+ "spanEnd": 150
770
+ }]
771
+ }
772
+ ```
773
+
774
+ **신규 (권장)**:
775
+ ```json
776
+ {
777
+ "evidence": [{
778
+ "messageIndex": 3,
779
+ "quote": "content JSONB → JSON"
780
+ }]
781
+ }
782
+ ```
783
+
784
+ ### 2.3 정렬 알고리즘
785
+
786
+ ```typescript
787
+ interface AlignmentStep {
788
+ method: 'exact' | 'normalized' | 'fuzzy';
789
+ description: string;
790
+ }
791
+
792
+ const ALIGNMENT_STEPS: AlignmentStep[] = [
793
+ { method: 'exact', description: '정확한 substring 매칭' },
794
+ { method: 'normalized', description: '공백/개행 정규화 후 매칭' },
795
+ { method: 'fuzzy', description: 'Levenshtein 거리 기반 유사 매칭 (threshold: 0.85)' }
796
+ ];
797
+ ```
798
+
799
+ ## 3. 입출력 스키마
800
+
801
+ ### 3.1 입력
802
+
803
+ ```typescript
804
+ interface AlignInput {
805
+ sessionMessages: string[]; // 원문 메시지 배열
806
+ extractedJson: ExtractedData; // LLM 추출 결과
807
+ }
808
+
809
+ interface ExtractedEvidence {
810
+ messageIndex: number;
811
+ quote: string; // 30~200자 권장
812
+ }
813
+ ```
814
+
815
+ ### 3.2 출력
816
+
817
+ ```typescript
818
+ interface AlignedEvidence {
819
+ messageIndex: number;
820
+ quote: string;
821
+ quoteHash: string; // SHA256(quote)
822
+ spanStart: number; // 원문 내 시작 위치
823
+ spanEnd: number; // 원문 내 끝 위치
824
+ confidence: number; // 0.0 ~ 1.0
825
+ matchMethod: 'exact' | 'normalized' | 'fuzzy' | 'none';
826
+ }
827
+
828
+ interface AlignResult {
829
+ evidenceAligned: boolean; // 모든 evidence가 정렬됨
830
+ alignedEvidence: AlignedEvidence[];
831
+ failedQuotes: string[]; // 정렬 실패한 quote 목록
832
+ }
833
+ ```
834
+
835
+ ## 4. 정렬 로직 상세
836
+
837
+ ### 4.1 Exact Match
838
+
839
+ ```typescript
840
+ function exactMatch(quote: string, source: string): AlignedSpan | null {
841
+ const index = source.indexOf(quote);
842
+ if (index === -1) return null;
843
+
844
+ return {
845
+ spanStart: index,
846
+ spanEnd: index + quote.length,
847
+ confidence: 1.0,
848
+ matchMethod: 'exact'
849
+ };
850
+ }
851
+ ```
852
+
853
+ ### 4.2 Normalized Match
854
+
855
+ ```typescript
856
+ function normalizedMatch(quote: string, source: string): AlignedSpan | null {
857
+ const normalizedQuote = normalize(quote);
858
+ const normalizedSource = normalize(source);
859
+
860
+ const index = normalizedSource.indexOf(normalizedQuote);
861
+ if (index === -1) return null;
862
+
863
+ // 원본 source에서 실제 위치 역추적 필요
864
+ const originalSpan = mapToOriginal(source, normalizedSource, index, normalizedQuote.length);
865
+
866
+ return {
867
+ ...originalSpan,
868
+ confidence: 0.95,
869
+ matchMethod: 'normalized'
870
+ };
871
+ }
872
+
873
+ function normalize(text: string): string {
874
+ return text
875
+ .replace(/\s+/g, ' ') // 연속 공백 → 단일 공백
876
+ .replace(/\n+/g, ' ') // 개행 → 공백
877
+ .trim()
878
+ .toLowerCase();
879
+ }
880
+ ```
881
+
882
+ ### 4.3 Fuzzy Match
883
+
884
+ ```typescript
885
+ function fuzzyMatch(
886
+ quote: string,
887
+ source: string,
888
+ threshold: number = 0.85
889
+ ): AlignedSpan | null {
890
+ const normalizedQuote = normalize(quote);
891
+ const windowSize = Math.ceil(normalizedQuote.length * 1.2);
892
+
893
+ let bestMatch: { start: number; end: number; score: number } | null = null;
894
+
895
+ // 슬라이딩 윈도우로 유사도 검사
896
+ for (let i = 0; i <= source.length - windowSize; i++) {
897
+ const window = normalize(source.slice(i, i + windowSize));
898
+ const score = levenshteinSimilarity(normalizedQuote, window);
899
+
900
+ if (score >= threshold && (!bestMatch || score > bestMatch.score)) {
901
+ bestMatch = { start: i, end: i + windowSize, score };
902
+ }
903
+ }
904
+
905
+ if (!bestMatch) return null;
906
+
907
+ return {
908
+ spanStart: bestMatch.start,
909
+ spanEnd: bestMatch.end,
910
+ confidence: bestMatch.score,
911
+ matchMethod: 'fuzzy'
912
+ };
913
+ }
914
+ ```
915
+
916
+ ## 5. 이벤트 스키마
917
+
918
+ ### 5.1 evidence_aligned 이벤트
919
+
920
+ ```typescript
921
+ interface EvidenceAlignedEvent {
922
+ event_type: 'evidence_aligned';
923
+ session_id: string;
924
+ payload: {
925
+ source_event_id: string; // session_ingested event
926
+ extraction_event_id: string; // memory_extracted event
927
+ entry_id: string;
928
+ aligned_count: number;
929
+ failed_count: number;
930
+ evidence: AlignedEvidence[];
931
+ failed_quotes: string[];
932
+ };
933
+ }
934
+ ```
935
+
936
+ ## 6. Idris2 영감 적용
937
+
938
+ ### 6.1 증거 기반 타입 (Proof-Carrying)
939
+
940
+ **Idris2 개념**:
941
+ ```idris
942
+ -- 타입이 증거를 포함
943
+ data EvidencedFact : Type where
944
+ MkFact : (claim : String) -> (proof : Span) -> EvidencedFact
945
+ ```
946
+
947
+ **TypeScript 적용**:
948
+ ```typescript
949
+ // Discriminated Union으로 증거 유무 구분
950
+ type Evidence =
951
+ | { aligned: true; span: AlignedSpan }
952
+ | { aligned: false; failureReason: string };
953
+
954
+ // 증거가 있는 entry만 Verified로 승격 가능
955
+ type VerifiedEntry = {
956
+ evidence: Extract<Evidence, { aligned: true }>[]; // 모든 evidence가 aligned
957
+ };
958
+ ```
959
+
960
+ ### 6.2 불변식
961
+
962
+ ```typescript
963
+ // Zod로 불변식 검증
964
+ const AlignedEvidenceSchema = z.object({
965
+ confidence: z.number().min(0).max(1),
966
+ matchMethod: z.enum(['exact', 'normalized', 'fuzzy']),
967
+ // fuzzy면 confidence < 1.0
968
+ }).refine(
969
+ (e) => e.matchMethod !== 'exact' || e.confidence === 1.0,
970
+ { message: 'Exact match must have confidence 1.0' }
971
+ );
972
+ ```
973
+
974
+ ## 7. 승격 정책
975
+
976
+ ### 7.1 Evidence 기반 승격 조건
977
+
978
+ | Stage | Evidence 요구사항 |
979
+ |-------|------------------|
980
+ | raw → working | 없음 |
981
+ | working → candidate | 없음 |
982
+ | candidate → verified | **evidenceAligned=true** 필수 |
983
+ | verified → certified | 추가 검증 필요 |
984
+
985
+ ### 7.2 실패 처리
986
+
987
+ ```typescript
988
+ async function processEntry(entry: Entry): Promise<void> {
989
+ const alignResult = await aligner.align(sessionMessages, entry.evidence);
990
+
991
+ if (!alignResult.evidenceAligned) {
992
+ // Verified 승격 금지
993
+ entry.meta.promotionBlocked = true;
994
+ entry.meta.promotionBlockReason = 'Evidence alignment failed';
995
+ entry.meta.failedQuotes = alignResult.failedQuotes;
996
+ }
997
+ }
998
+ ```
999
+
1000
+ ## 8. 기존 EvidenceAligner와 차이점
1001
+
1002
+ ### 8.1 현재 구현 (src/core/evidence-aligner.ts)
1003
+
1004
+ ```typescript
1005
+ // 현재: quote 기반 정렬 지원
1006
+ align(claims: string[], sourceContent: string): AlignmentResult {
1007
+ // exact match만 지원
1008
+ const exactSpan = this.findExactMatch(claim, sourceContent);
1009
+ }
1010
+ ```
1011
+
1012
+ ### 8.2 V2 개선사항
1013
+
1014
+ | 항목 | 현재 | V2 |
1015
+ |------|-----|-----|
1016
+ | 정규화 매칭 | 없음 | 공백/개행 정규화 |
1017
+ | Fuzzy 매칭 | 없음 | Levenshtein 기반 (threshold 0.85) |
1018
+ | 이벤트 기록 | 없음 | evidence_aligned 이벤트 발행 |
1019
+ | 승격 연동 | 없음 | evidenceAligned → Verified 조건 |
1020
+ | 메시지 인덱스 | 없음 | messageIndex 기반 정확한 소스 식별 |
1021
+
1022
+ ## 9. 성공 기준
1023
+
1024
+ - [ ] LLM Extractor가 quote만 출력하도록 프롬프트 수정
1025
+ - [ ] EvidenceAligner가 3단계 정렬 (exact → normalized → fuzzy) 수행
1026
+ - [ ] 정렬 결과가 evidence_aligned 이벤트로 기록됨
1027
+ - [ ] evidenceAligned=false인 entry는 Verified 승격 불가
1028
+ - [ ] 기존 evidence-aligner.ts와 호환 유지