claude-memory-layer 1.0.0
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.
- package/.claude-plugin/commands/memory-forget.md +42 -0
- package/.claude-plugin/commands/memory-history.md +34 -0
- package/.claude-plugin/commands/memory-import.md +56 -0
- package/.claude-plugin/commands/memory-list.md +37 -0
- package/.claude-plugin/commands/memory-search.md +36 -0
- package/.claude-plugin/commands/memory-stats.md +34 -0
- package/.claude-plugin/hooks.json +59 -0
- package/.claude-plugin/plugin.json +24 -0
- package/.history/package_20260201112328.json +45 -0
- package/.history/package_20260201113602.json +45 -0
- package/.history/package_20260201113713.json +45 -0
- package/.history/package_20260201114110.json +45 -0
- package/Memo.txt +558 -0
- package/README.md +520 -0
- package/context.md +636 -0
- package/dist/.claude-plugin/commands/memory-forget.md +42 -0
- package/dist/.claude-plugin/commands/memory-history.md +34 -0
- package/dist/.claude-plugin/commands/memory-import.md +56 -0
- package/dist/.claude-plugin/commands/memory-list.md +37 -0
- package/dist/.claude-plugin/commands/memory-search.md +36 -0
- package/dist/.claude-plugin/commands/memory-stats.md +34 -0
- package/dist/.claude-plugin/hooks.json +59 -0
- package/dist/.claude-plugin/plugin.json +24 -0
- package/dist/cli/index.js +3539 -0
- package/dist/cli/index.js.map +7 -0
- package/dist/core/index.js +4408 -0
- package/dist/core/index.js.map +7 -0
- package/dist/hooks/session-end.js +2971 -0
- package/dist/hooks/session-end.js.map +7 -0
- package/dist/hooks/session-start.js +2969 -0
- package/dist/hooks/session-start.js.map +7 -0
- package/dist/hooks/stop.js +3123 -0
- package/dist/hooks/stop.js.map +7 -0
- package/dist/hooks/user-prompt-submit.js +2960 -0
- package/dist/hooks/user-prompt-submit.js.map +7 -0
- package/dist/services/memory-service.js +2931 -0
- package/dist/services/memory-service.js.map +7 -0
- package/package.json +45 -0
- package/plan.md +1642 -0
- package/scripts/build.ts +102 -0
- package/spec.md +624 -0
- package/specs/citations-system/context.md +243 -0
- package/specs/citations-system/plan.md +495 -0
- package/specs/citations-system/spec.md +371 -0
- package/specs/endless-mode/context.md +305 -0
- package/specs/endless-mode/plan.md +620 -0
- package/specs/endless-mode/spec.md +455 -0
- package/specs/entity-edge-model/context.md +401 -0
- package/specs/entity-edge-model/plan.md +459 -0
- package/specs/entity-edge-model/spec.md +391 -0
- package/specs/evidence-aligner-v2/context.md +401 -0
- package/specs/evidence-aligner-v2/plan.md +303 -0
- package/specs/evidence-aligner-v2/spec.md +312 -0
- package/specs/mcp-desktop-integration/context.md +278 -0
- package/specs/mcp-desktop-integration/plan.md +550 -0
- package/specs/mcp-desktop-integration/spec.md +494 -0
- package/specs/post-tool-use-hook/context.md +319 -0
- package/specs/post-tool-use-hook/plan.md +469 -0
- package/specs/post-tool-use-hook/spec.md +364 -0
- package/specs/private-tags/context.md +288 -0
- package/specs/private-tags/plan.md +412 -0
- package/specs/private-tags/spec.md +345 -0
- package/specs/progressive-disclosure/context.md +346 -0
- package/specs/progressive-disclosure/plan.md +663 -0
- package/specs/progressive-disclosure/spec.md +415 -0
- package/specs/task-entity-system/context.md +297 -0
- package/specs/task-entity-system/plan.md +301 -0
- package/specs/task-entity-system/spec.md +314 -0
- package/specs/vector-outbox-v2/context.md +470 -0
- package/specs/vector-outbox-v2/plan.md +562 -0
- package/specs/vector-outbox-v2/spec.md +466 -0
- package/specs/web-viewer-ui/context.md +384 -0
- package/specs/web-viewer-ui/plan.md +797 -0
- package/specs/web-viewer-ui/spec.md +516 -0
- package/src/cli/index.ts +570 -0
- package/src/core/canonical-key.ts +186 -0
- package/src/core/citation-generator.ts +63 -0
- package/src/core/consolidated-store.ts +279 -0
- package/src/core/consolidation-worker.ts +384 -0
- package/src/core/context-formatter.ts +276 -0
- package/src/core/continuity-manager.ts +336 -0
- package/src/core/edge-repo.ts +324 -0
- package/src/core/embedder.ts +124 -0
- package/src/core/entity-repo.ts +342 -0
- package/src/core/event-store.ts +672 -0
- package/src/core/evidence-aligner.ts +635 -0
- package/src/core/graduation.ts +365 -0
- package/src/core/index.ts +32 -0
- package/src/core/matcher.ts +210 -0
- package/src/core/metadata-extractor.ts +203 -0
- package/src/core/privacy/filter.ts +179 -0
- package/src/core/privacy/index.ts +20 -0
- package/src/core/privacy/tag-parser.ts +145 -0
- package/src/core/progressive-retriever.ts +415 -0
- package/src/core/retriever.ts +235 -0
- package/src/core/task/blocker-resolver.ts +325 -0
- package/src/core/task/index.ts +9 -0
- package/src/core/task/task-matcher.ts +238 -0
- package/src/core/task/task-projector.ts +345 -0
- package/src/core/task/task-resolver.ts +414 -0
- package/src/core/types.ts +841 -0
- package/src/core/vector-outbox.ts +295 -0
- package/src/core/vector-store.ts +182 -0
- package/src/core/vector-worker.ts +488 -0
- package/src/core/working-set-store.ts +244 -0
- package/src/hooks/post-tool-use.ts +127 -0
- package/src/hooks/session-end.ts +78 -0
- package/src/hooks/session-start.ts +57 -0
- package/src/hooks/stop.ts +78 -0
- package/src/hooks/user-prompt-submit.ts +54 -0
- package/src/mcp/handlers.ts +212 -0
- package/src/mcp/index.ts +47 -0
- package/src/mcp/tools.ts +78 -0
- package/src/server/api/citations.ts +101 -0
- package/src/server/api/events.ts +101 -0
- package/src/server/api/index.ts +18 -0
- package/src/server/api/search.ts +98 -0
- package/src/server/api/sessions.ts +111 -0
- package/src/server/api/stats.ts +97 -0
- package/src/server/index.ts +91 -0
- package/src/services/memory-service.ts +626 -0
- package/src/services/session-history-importer.ts +367 -0
- package/tests/canonical-key.test.ts +101 -0
- package/tests/evidence-aligner.test.ts +152 -0
- package/tests/matcher.test.ts +112 -0
- package/tsconfig.json +24 -0
- package/vitest.config.ts +15 -0
|
@@ -0,0 +1,319 @@
|
|
|
1
|
+
# PostToolUse Hook Context
|
|
2
|
+
|
|
3
|
+
> **Version**: 1.0.0
|
|
4
|
+
> **Created**: 2026-02-01
|
|
5
|
+
|
|
6
|
+
## 1. 배경
|
|
7
|
+
|
|
8
|
+
### 1.1 claude-mem의 접근 방식
|
|
9
|
+
|
|
10
|
+
claude-mem 프로젝트는 5개의 라이프사이클 훅을 사용:
|
|
11
|
+
|
|
12
|
+
```
|
|
13
|
+
SessionStart → UserPromptSubmit → PostToolUse → Stop → SessionEnd
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
**PostToolUse 훅의 역할** (claude-mem):
|
|
17
|
+
- 모든 도구 실행 결과를 "observation"으로 캡처
|
|
18
|
+
- SQLite + Chroma에 저장하여 검색 가능하게 함
|
|
19
|
+
- 세션 간 도구 사용 패턴 학습
|
|
20
|
+
|
|
21
|
+
**주요 특징**:
|
|
22
|
+
- 웹 뷰어에서 실시간 observation 스트림 표시
|
|
23
|
+
- 도구별로 다른 압축 전략 적용
|
|
24
|
+
- `<private>` 태그로 민감 정보 제외
|
|
25
|
+
|
|
26
|
+
### 1.2 현재 code-memory의 상황
|
|
27
|
+
|
|
28
|
+
현재 4개 훅만 구현:
|
|
29
|
+
|
|
30
|
+
```
|
|
31
|
+
SessionStart → UserPromptSubmit → Stop → SessionEnd
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
**부족한 점**:
|
|
35
|
+
1. 도구 실행 결과가 Stop 훅에서 응답 전체로만 저장됨
|
|
36
|
+
2. 개별 도구 실행의 입력/출력 분리 불가
|
|
37
|
+
3. "어떤 도구를 어떻게 사용했는지" 패턴 학습 불가
|
|
38
|
+
|
|
39
|
+
### 1.3 도입 필요성
|
|
40
|
+
|
|
41
|
+
| 현재 상황 | PostToolUse 도입 후 |
|
|
42
|
+
|----------|-------------------|
|
|
43
|
+
| 응답 전체만 저장 | 도구별 개별 저장 |
|
|
44
|
+
| 파일 경로 추출 어려움 | 메타데이터로 즉시 조회 |
|
|
45
|
+
| 성공/실패 구분 없음 | success 플래그로 분리 |
|
|
46
|
+
| 검색 시 노이즈 많음 | 도구 타입별 필터링 가능 |
|
|
47
|
+
|
|
48
|
+
## 2. Claude Code 훅 시스템
|
|
49
|
+
|
|
50
|
+
### 2.1 지원되는 훅 이벤트
|
|
51
|
+
|
|
52
|
+
Claude Code가 지원하는 훅 이벤트 목록:
|
|
53
|
+
|
|
54
|
+
```typescript
|
|
55
|
+
type HookEvent =
|
|
56
|
+
| 'session-start' // 세션 시작
|
|
57
|
+
| 'user-prompt-submit' // 사용자 프롬프트 제출
|
|
58
|
+
| 'post-tool-use' // 도구 사용 후 (NEW)
|
|
59
|
+
| 'stop' // 에이전트 응답 완료
|
|
60
|
+
| 'session-end'; // 세션 종료
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
### 2.2 post-tool-use 훅 입력 형식
|
|
64
|
+
|
|
65
|
+
Claude Code가 전달하는 데이터:
|
|
66
|
+
|
|
67
|
+
```typescript
|
|
68
|
+
interface PostToolUseHookInput {
|
|
69
|
+
// 도구 정보
|
|
70
|
+
tool_name: string; // "Read", "Write", "Bash" 등
|
|
71
|
+
tool_input: object; // 도구에 전달된 파라미터
|
|
72
|
+
tool_output: string; // 도구 실행 결과
|
|
73
|
+
tool_error?: string; // 에러 발생 시 메시지
|
|
74
|
+
|
|
75
|
+
// 세션 정보
|
|
76
|
+
session_id: string;
|
|
77
|
+
conversation_id: string;
|
|
78
|
+
|
|
79
|
+
// 타이밍
|
|
80
|
+
started_at: string; // ISO 8601 형식
|
|
81
|
+
ended_at: string;
|
|
82
|
+
}
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
### 2.3 훅 등록 방법
|
|
86
|
+
|
|
87
|
+
`.claude-plugin/hooks.json`:
|
|
88
|
+
|
|
89
|
+
```json
|
|
90
|
+
{
|
|
91
|
+
"hooks": [
|
|
92
|
+
{
|
|
93
|
+
"event": "post-tool-use",
|
|
94
|
+
"script": "node dist/hooks/post-tool-use.js",
|
|
95
|
+
"timeout": 5000
|
|
96
|
+
}
|
|
97
|
+
]
|
|
98
|
+
}
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
## 3. 참고 구현
|
|
102
|
+
|
|
103
|
+
### 3.1 claude-mem의 observation 저장
|
|
104
|
+
|
|
105
|
+
```typescript
|
|
106
|
+
// claude-mem의 접근 방식 (추정)
|
|
107
|
+
interface Observation {
|
|
108
|
+
id: string;
|
|
109
|
+
session_id: string;
|
|
110
|
+
tool_name: string;
|
|
111
|
+
input: object;
|
|
112
|
+
output: string;
|
|
113
|
+
timestamp: Date;
|
|
114
|
+
|
|
115
|
+
// 검색을 위한 필드
|
|
116
|
+
summary: string; // LLM으로 생성한 요약
|
|
117
|
+
embedding: number[]; // 벡터 임베딩
|
|
118
|
+
}
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
### 3.2 도구별 처리 차이
|
|
122
|
+
|
|
123
|
+
| 도구 | 저장 내용 | 요약 생성 |
|
|
124
|
+
|------|----------|----------|
|
|
125
|
+
| Read | 파일 경로, 첫 N줄 | "Read {path}: {first_line}..." |
|
|
126
|
+
| Write | 파일 경로, 변경 내용 요약 | "Wrote {lines} lines to {path}" |
|
|
127
|
+
| Bash | 명령어, exit code | "Ran `{cmd}`: {status}" |
|
|
128
|
+
| Grep | 패턴, 매칭 파일 수 | "Found {n} matches for {pattern}" |
|
|
129
|
+
|
|
130
|
+
### 3.3 Progressive Disclosure 활용
|
|
131
|
+
|
|
132
|
+
```typescript
|
|
133
|
+
// 검색 시 observation을 3단계로 반환
|
|
134
|
+
interface ObservationSearchResult {
|
|
135
|
+
// Layer 1: ID + 요약 (최소 토큰)
|
|
136
|
+
layer1: { id: string; summary: string }[];
|
|
137
|
+
|
|
138
|
+
// Layer 2: 타임라인 컨텍스트
|
|
139
|
+
layer2: (ids: string[]) => {
|
|
140
|
+
id: string;
|
|
141
|
+
tool_name: string;
|
|
142
|
+
timestamp: Date;
|
|
143
|
+
}[];
|
|
144
|
+
|
|
145
|
+
// Layer 3: 전체 내용
|
|
146
|
+
layer3: (ids: string[]) => Observation[];
|
|
147
|
+
}
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
## 4. 기존 코드와의 관계
|
|
151
|
+
|
|
152
|
+
### 4.1 event-store.ts
|
|
153
|
+
|
|
154
|
+
현재 EventStore 스키마:
|
|
155
|
+
|
|
156
|
+
```sql
|
|
157
|
+
CREATE TABLE events (
|
|
158
|
+
event_id VARCHAR PRIMARY KEY,
|
|
159
|
+
event_type VARCHAR NOT NULL,
|
|
160
|
+
session_id VARCHAR NOT NULL,
|
|
161
|
+
timestamp TIMESTAMP NOT NULL,
|
|
162
|
+
payload_json JSON NOT NULL
|
|
163
|
+
);
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
**확장 필요 없음** - `event_type = 'tool_observation'`으로 저장 가능
|
|
167
|
+
|
|
168
|
+
### 4.2 types.ts
|
|
169
|
+
|
|
170
|
+
추가해야 할 타입:
|
|
171
|
+
|
|
172
|
+
```typescript
|
|
173
|
+
// 기존 EventType
|
|
174
|
+
export const EventTypeSchema = z.enum([
|
|
175
|
+
'user_prompt',
|
|
176
|
+
'assistant_response',
|
|
177
|
+
'session_start',
|
|
178
|
+
'session_end',
|
|
179
|
+
'tool_observation' // 추가
|
|
180
|
+
]);
|
|
181
|
+
|
|
182
|
+
// ToolObservationPayload 추가
|
|
183
|
+
export const ToolObservationPayloadSchema = z.object({
|
|
184
|
+
toolName: z.string(),
|
|
185
|
+
toolInput: z.record(z.unknown()),
|
|
186
|
+
toolOutput: z.string(),
|
|
187
|
+
// ...
|
|
188
|
+
});
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
### 4.3 embedder.ts
|
|
192
|
+
|
|
193
|
+
현재 임베딩 대상:
|
|
194
|
+
|
|
195
|
+
```typescript
|
|
196
|
+
function getEmbeddingContent(event: Event): string {
|
|
197
|
+
switch (event.eventType) {
|
|
198
|
+
case 'user_prompt':
|
|
199
|
+
return event.payload.content;
|
|
200
|
+
case 'assistant_response':
|
|
201
|
+
return event.payload.content;
|
|
202
|
+
// tool_observation 추가 필요
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
### 4.4 retriever.ts
|
|
208
|
+
|
|
209
|
+
검색 인터페이스 확장:
|
|
210
|
+
|
|
211
|
+
```typescript
|
|
212
|
+
// 기존
|
|
213
|
+
async search(query: string): Promise<SearchResult[]>;
|
|
214
|
+
|
|
215
|
+
// 추가
|
|
216
|
+
async searchToolHistory(query: string): Promise<ToolObservation[]>;
|
|
217
|
+
async getToolsForFile(filePath: string): Promise<ToolObservation[]>;
|
|
218
|
+
```
|
|
219
|
+
|
|
220
|
+
## 5. 설계 결정 사항
|
|
221
|
+
|
|
222
|
+
### 5.1 왜 별도 이벤트 타입인가?
|
|
223
|
+
|
|
224
|
+
**대안 1: assistant_response에 포함**
|
|
225
|
+
```json
|
|
226
|
+
{
|
|
227
|
+
"eventType": "assistant_response",
|
|
228
|
+
"payload": {
|
|
229
|
+
"content": "...",
|
|
230
|
+
"tools_used": [{ "name": "Read", "output": "..." }]
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
```
|
|
234
|
+
- 단점: 도구별 검색 어려움, 출력이 커짐
|
|
235
|
+
|
|
236
|
+
**대안 2: 별도 테이블**
|
|
237
|
+
```sql
|
|
238
|
+
CREATE TABLE tool_observations (...);
|
|
239
|
+
```
|
|
240
|
+
- 단점: 스키마 변경 필요, 일관성 관리 복잡
|
|
241
|
+
|
|
242
|
+
**선택: 기존 events 테이블에 새 event_type**
|
|
243
|
+
- 장점: 스키마 변경 없음
|
|
244
|
+
- 장점: 기존 쿼리 패턴 재사용
|
|
245
|
+
- 장점: 벡터 임베딩 파이프라인 공유
|
|
246
|
+
|
|
247
|
+
### 5.2 출력 압축 전략
|
|
248
|
+
|
|
249
|
+
**문제**: 도구 출력이 매우 클 수 있음 (대용량 파일 읽기)
|
|
250
|
+
|
|
251
|
+
**해결**:
|
|
252
|
+
1. 크기 제한 (10KB)
|
|
253
|
+
2. 줄 수 제한 (100줄)
|
|
254
|
+
3. Head + Tail 방식 (앞 50줄 + 뒤 50줄)
|
|
255
|
+
4. 도구별 특화 압축 (Grep은 파일 목록만)
|
|
256
|
+
|
|
257
|
+
### 5.3 민감 정보 처리
|
|
258
|
+
|
|
259
|
+
**현재 privacy.excludePatterns 활용**:
|
|
260
|
+
```typescript
|
|
261
|
+
const patterns = config.privacy.excludePatterns;
|
|
262
|
+
// ['password', 'secret', 'api_key']
|
|
263
|
+
```
|
|
264
|
+
|
|
265
|
+
**추가 필요**:
|
|
266
|
+
- 정규식 기반 마스킹
|
|
267
|
+
- 환경 변수 값 감지
|
|
268
|
+
- Bearer 토큰 패턴
|
|
269
|
+
|
|
270
|
+
### 5.4 임베딩 vs 저장만
|
|
271
|
+
|
|
272
|
+
**질문**: 모든 tool_observation을 임베딩해야 하나?
|
|
273
|
+
|
|
274
|
+
**결론**: 선택적 임베딩
|
|
275
|
+
- Read/Write: 파일 경로 + 첫 줄만 임베딩
|
|
276
|
+
- Bash: 명령어만 임베딩
|
|
277
|
+
- 전체 출력은 검색되지 않음 (너무 노이즈가 많음)
|
|
278
|
+
|
|
279
|
+
## 6. 성능 고려사항
|
|
280
|
+
|
|
281
|
+
### 6.1 훅 실행 시간
|
|
282
|
+
|
|
283
|
+
**제약**: 훅은 빨리 반환되어야 함 (Claude 응답 지연 방지)
|
|
284
|
+
|
|
285
|
+
**해결**:
|
|
286
|
+
```typescript
|
|
287
|
+
// 비동기 저장 (fire-and-forget)
|
|
288
|
+
async function handlePostToolUse(input: PostToolUseInput): Promise<void> {
|
|
289
|
+
// 바로 반환, 저장은 백그라운드
|
|
290
|
+
setImmediate(async () => {
|
|
291
|
+
await storeObservation(input);
|
|
292
|
+
});
|
|
293
|
+
}
|
|
294
|
+
```
|
|
295
|
+
|
|
296
|
+
### 6.2 저장 빈도
|
|
297
|
+
|
|
298
|
+
**문제**: 대화 중 수십 개의 도구가 실행될 수 있음
|
|
299
|
+
|
|
300
|
+
**해결**:
|
|
301
|
+
1. 배치 저장 (10개씩 모아서)
|
|
302
|
+
2. 중복 제거 (같은 파일 연속 Read)
|
|
303
|
+
3. 샘플링 (Glob 결과는 요약만)
|
|
304
|
+
|
|
305
|
+
### 6.3 벡터 저장소 크기
|
|
306
|
+
|
|
307
|
+
**문제**: tool_observation이 많아지면 벡터 DB 비대
|
|
308
|
+
|
|
309
|
+
**해결**:
|
|
310
|
+
1. TTL 적용 (30일 후 삭제)
|
|
311
|
+
2. 자주 사용되는 것만 임베딩 유지
|
|
312
|
+
3. graduation 적용 (L0에서 L4로 승급하는 것만 유지)
|
|
313
|
+
|
|
314
|
+
## 7. 참고 자료
|
|
315
|
+
|
|
316
|
+
- **claude-mem README**: 5 lifecycle hooks, observation storage
|
|
317
|
+
- **Claude Code Hooks**: 공식 훅 이벤트 문서
|
|
318
|
+
- **AXIOMMIND**: Principle 2 (Append-Only)
|
|
319
|
+
- **기존 specs**: entity-edge-model, vector-outbox-v2
|