925d91a4e5
ASTRA 자기 제안(검증 통과분) 1순위 구현: 충돌을 '감지'에서 '재구성'으로. [충돌 → 통합 초안 (사람 승인 대기)] - conflictScan: 모순 감지 시 LLM이 통합 초안 생성 → .astra/growth/reconcile/ (런당 ≤3건). 신뢰 권고 우선 쪽 기준 + 타방의 유효 정보 보존 + 판단 불가 사실은 "(확인 필요: A는 X, B는 Y)" 병기 + 출처 표기 강제. - 자동 반영 절대 없음 — 초안 머리에 명기, 승인 시 사람이 직접 반영 (status: pending-review). 거부 = 파일 삭제. [A-MEM 레슨 네트워크 (NeurIPS 2025 이식, deep research 2순위)] - lessonNetwork.ts: 새 레슨 저장 시 기존 레슨과 토큰 자카드 유사도 상위 3개를 "## 관련 레슨" [[위키링크]]로 연결 + 기존 레슨에 백링크(역방향 갱신 — memory evolution). LLM 호출 0 — 캡처 경로 지연 없음. 멱등(재실행 안전). - 연결 지점: Correction Loop 자동 레슨 + 수동 레슨 생성(Astra: New Lesson). 고립된 카드 모음 → 상호 연결 네트워크: "같은 종류의 실수" 패턴이 파일 수준에서 보이고 RAG 위키링크로 함께 검색됨. 테스트 6건 추가 (유사도·링크 멱등·양방향·초안 형식). 전체 588 통과. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
141 lines
7.5 KiB
TypeScript
141 lines
7.5 KiB
TypeScript
import * as fs from 'fs';
|
||
import * as path from 'path';
|
||
import * as vscode from 'vscode';
|
||
import { findBrainFiles, getActiveBrainProfile, openInEditorGroup } from '../utils';
|
||
import { getBrainTokenIndex } from '../retrieval';
|
||
import { lessonTemplate, lessonSlug, parseLessonFrontmatter, normalizeLessonTitle, bumpLessonOccurrences } from '../retrieval/lessonHelpers';
|
||
|
||
/**
|
||
* Experience Memory — lesson card 의 *대화형* 명령 (create / manage).
|
||
*
|
||
* 옛 코드: extension.ts 의 `activate()` 안 nested function 3개 (listLessonFiles,
|
||
* createLessonCard, manageLessons). 그러나 셋 다 `activate()` 클로저에서 캡처하는
|
||
* 게 하나도 없었음 (모두 module-level import 만 사용) — nested 인 이유 없음.
|
||
* 분리해 (a) extension.ts 의 activate() 길이 축소, (b) lesson 워크플로우의 단위
|
||
* 테스트 가능, (c) 다른 곳 (예: chat handler) 에서도 직접 호출 가능.
|
||
*/
|
||
|
||
/** All lesson/playbook/qa-finding cards in the active brain. Uses the brain index for the lesson-kind
|
||
* filter (cheap when warm), then reads only those few files for their frontmatter title/occurrences. */
|
||
function listLessonFiles(brainDir: string): Array<{ filePath: string; rel: string; title: string; kind: string; occurrences: number }> {
|
||
const out: Array<{ filePath: string; rel: string; title: string; kind: string; occurrences: number }> = [];
|
||
let files: string[] = [];
|
||
try { files = findBrainFiles(brainDir); } catch { return out; }
|
||
for (const d of getBrainTokenIndex(brainDir, files)) {
|
||
if (!d.kind) continue;
|
||
let content = '';
|
||
try { content = fs.readFileSync(d.filePath, 'utf8').slice(0, 4000); } catch { continue; }
|
||
const fm = parseLessonFrontmatter(content);
|
||
out.push({ filePath: d.filePath, rel: d.relativePath, title: (fm.title || d.title).trim(), kind: d.kind, occurrences: fm.occurrences ?? 1 });
|
||
}
|
||
return out.sort((a, b) => a.rel.localeCompare(b.rel));
|
||
}
|
||
|
||
/** Shared lesson-card creator used by the lesson commands. Dedup-merges into an existing lesson with the same title. */
|
||
export async function createLessonCard(situation?: string): Promise<void> {
|
||
const brain = getActiveBrainProfile();
|
||
const brainDir = brain?.localBrainPath;
|
||
if (!brainDir || !path.isAbsolute(brainDir)) {
|
||
vscode.window.showErrorMessage('활성 Second Brain 경로가 설정되지 않았습니다. Settings에서 localBrainPath / brainProfiles를 먼저 설정하세요.');
|
||
return;
|
||
}
|
||
const title = (await vscode.window.showInputBox({
|
||
title: 'New Lesson — Experience Memory',
|
||
prompt: '이 교훈의 제목 (예: "Telegram 원격 실행은 allowlist 필수")',
|
||
placeHolder: '한 줄 요약 — 다음에 같은 실수를 안 하려면 뭘 기억해야 하나',
|
||
ignoreFocusOut: true,
|
||
}))?.trim();
|
||
if (!title) return;
|
||
const today = new Date().toISOString().slice(0, 10);
|
||
|
||
// Dedup-merge: a recurring mistake should get LOUDER (occurrences++), not spawn a duplicate card.
|
||
const norm = normalizeLessonTitle(title);
|
||
const existing = norm ? listLessonFiles(brainDir).find((l) => normalizeLessonTitle(l.title) === norm) : undefined;
|
||
if (existing) {
|
||
const pick = await vscode.window.showInformationMessage(
|
||
`이미 같은 제목의 교훈이 있습니다: "${existing.title}" (occurrences: ${existing.occurrences}). 갱신할까요?`,
|
||
{ modal: false },
|
||
'갱신 (occurrences +1)', '새로 만들기',
|
||
);
|
||
if (!pick) return;
|
||
if (pick === '갱신 (occurrences +1)') {
|
||
try {
|
||
const cur = fs.readFileSync(existing.filePath, 'utf8');
|
||
fs.writeFileSync(existing.filePath, bumpLessonOccurrences(cur, today), 'utf8');
|
||
} catch (e: any) {
|
||
vscode.window.showErrorMessage(`교훈 갱신 실패: ${e?.message ?? e}`);
|
||
return;
|
||
}
|
||
await openInEditorGroup(existing.filePath);
|
||
vscode.window.showInformationMessage(`기존 교훈을 갱신했습니다 (occurrences: ${existing.occurrences + 1}). 필요하면 내용을 보강하세요.`);
|
||
return;
|
||
}
|
||
// else fall through and create a new one
|
||
}
|
||
|
||
const dir = path.join(brainDir, 'lessons');
|
||
try { fs.mkdirSync(dir, { recursive: true }); } catch { /* fall through to error below */ }
|
||
let filePath = path.join(dir, `${today}-${lessonSlug(title)}.md`);
|
||
let n = 2;
|
||
while (fs.existsSync(filePath)) { filePath = path.join(dir, `${today}-${lessonSlug(title)}-${n++}.md`); }
|
||
try {
|
||
fs.writeFileSync(filePath, lessonTemplate(title, today, situation), 'utf8');
|
||
// A-MEM 레슨 네트워크 — 비슷한 과거 교훈과 상호 링크 (실패 무해).
|
||
try {
|
||
const { linkRelatedLessons } = await import('../intelligence/lessonNetwork');
|
||
linkRelatedLessons(brainDir, filePath);
|
||
} catch { /* 링크 실패가 생성을 막지 않음 */ }
|
||
} catch (e: any) {
|
||
vscode.window.showErrorMessage(`교훈 파일 생성 실패: ${e?.message ?? e}`);
|
||
return;
|
||
}
|
||
await openInEditorGroup(filePath);
|
||
vscode.window.showInformationMessage('교훈 카드를 만들었습니다. 내용을 채워 저장하면 다음 유사 작업 시 자동으로 체크리스트에 들어갑니다.');
|
||
}
|
||
|
||
/** Browse lesson cards: open one, or delete one (trash button). Also the "manage" surface for ignoring bad lessons. */
|
||
export async function manageLessons(): Promise<void> {
|
||
const brain = getActiveBrainProfile();
|
||
const brainDir = brain?.localBrainPath;
|
||
if (!brainDir || !path.isAbsolute(brainDir)) {
|
||
vscode.window.showErrorMessage('활성 Second Brain 경로가 설정되지 않았습니다.');
|
||
return;
|
||
}
|
||
const lessons = listLessonFiles(brainDir);
|
||
if (lessons.length === 0) {
|
||
const make = await vscode.window.showInformationMessage('아직 교훈 카드가 없습니다.', '새 교훈 만들기');
|
||
if (make) await createLessonCard();
|
||
return;
|
||
}
|
||
const deleteBtn: vscode.QuickInputButton = { iconPath: new vscode.ThemeIcon('trash'), tooltip: '이 교훈 삭제' };
|
||
const qp = vscode.window.createQuickPick<vscode.QuickPickItem & { _file: string }>();
|
||
qp.title = 'Lessons — Experience Memory';
|
||
qp.placeholder = '교훈을 선택하면 열립니다. 휴지통 아이콘으로 삭제. (삭제 = 더 이상 주입 안 됨)';
|
||
qp.items = lessons.map((l) => ({
|
||
label: `$(${l.kind === 'playbook' ? 'book' : l.kind === 'qa-finding' ? 'bug' : 'lightbulb'}) ${l.title}`,
|
||
description: l.occurrences > 1 ? `×${l.occurrences}` : '',
|
||
detail: l.rel,
|
||
buttons: [deleteBtn],
|
||
_file: l.filePath,
|
||
}));
|
||
qp.onDidTriggerItemButton(async (e) => {
|
||
const file = e.item._file;
|
||
const ok = await vscode.window.showWarningMessage(`교훈 "${e.item.label}" 을(를) 삭제할까요?`, { modal: true }, '삭제');
|
||
if (ok === '삭제') {
|
||
try { fs.unlinkSync(file); } catch (err: any) { vscode.window.showErrorMessage(`삭제 실패: ${err?.message ?? err}`); return; }
|
||
qp.items = qp.items.filter((it) => it._file !== file);
|
||
vscode.window.showInformationMessage('교훈을 삭제했습니다.');
|
||
if (qp.items.length === 0) qp.hide();
|
||
}
|
||
});
|
||
qp.onDidAccept(async () => {
|
||
const sel = qp.selectedItems[0];
|
||
qp.hide();
|
||
if (sel) {
|
||
await openInEditorGroup(sel._file);
|
||
}
|
||
});
|
||
qp.onDidHide(() => qp.dispose());
|
||
qp.show();
|
||
}
|