leerness 1.12.1 → 1.13.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.
@@ -1,616 +1,620 @@
1
- // lib/session-close.js — session close 핸들러 (UR-0025/UR-0125 큰 핸들러 모듈화 10번째, 1.9.425)
2
- // bin/leerness.js 에서 sessionClose(599줄) 분리. DI: harness 고유 의존 다수 주입.
3
- // io 프리미티브는 ./io, _sanitizeFences/_parseArchiveBlocks 는 ./pure-utils, cp/os/path/fs 빌트인.
4
- // __filename→harnessPath(self-spawn). 동작/출력 무변경(9 카테고리 + 활성 룰 검증 + retro 등).
5
- 'use strict';
6
- const cp = require('child_process');
7
- const os = require('os');
8
- const path = require('path');
9
- const fs = require('fs');
10
- const { log, ok, warn, fail, failJson, today, now, absRoot, exists, read, readBuf, mkdirp, writeUtf8, append, rel } = require('./io');
11
- const { _sanitizeFences, _parseArchiveBlocks } = require('./pure-utils');
12
-
13
- function sessionClose(root, opts = {}, deps = {}) {
14
- const { VERSION, STATUSES, MARK, has, arg, harnessPath, readProgressRows, evidencePath, handoffPath, currentStatePath, taskLogPath, verifyRules, _autoRoadmap, _readUsageStats, readSessionCounter, writeSessionCounter, _retroAggregate, _retroOneLine, retroCmd, _loadDecisions, readRules, planPath, _loadLessons, _readFeatureGraph, _auditUserRequests, _detectDeliveredRequests, _computeRoundHistory, _computeMilestones, _computeRecentChanges, _collectPyFiles, _analyzePyFile, _collectRuntimeEnv, _scanShellScriptsEncoding, _listAPISkills, _matchAPISkills, _loadShellFailures, _shellEnvDrift, _runPreWakeAudit, _saveAndAppendPreWakeReport, _runIdempotencyAudit, _detectAbnormalShutdown, _updateUserRequest } = deps;
15
- root = absRoot(root);
16
- // 1.10.4 (13th 버그헌트 P2, UR-0167): 경로 없음/디렉토리 아님 → 구조화 에러 + exit 1. mkdir <path>/.harness ENOTDIR 크래시 & 실패를 성공(exit 0)으로 오판하던 문제 차단.
17
- if (!exists(root) || !fs.statSync(root).isDirectory()) { failJson(!!opts.json || has('--json'), 'path_not_found', `경로 없음 또는 디렉토리 아님: ${root}`); return; }
18
- // 1.9.103: --json 모드 — stdout 억제 후 구조화 출력
19
- const jsonMode = !!opts.json || has('--json');
20
- const _origWrite = process.stdout.write.bind(process.stdout);
21
- if (jsonMode) process.stdout.write = () => true;
22
- const jsonResult = { version: VERSION, root, closedAt: now() };
23
- try {
24
- const rows = readProgressRows(root);
25
- const buckets = {};
26
- for (const s of STATUSES) buckets[s] = [];
27
- for (const r of rows) (buckets[r.status] || (buckets[r.status] = [])).push(r);
28
- // 1.9.103: JSON 결과 누적
29
- jsonResult.taskCounts = {};
30
- for (const s of STATUSES) jsonResult.taskCounts[s] = (buckets[s] || []).length;
31
- jsonResult.recommendedDirection = (buckets['in-progress'][0]?.request) || (buckets['planned'][0]?.request) || (buckets['requested'][0]?.request) || null;
32
- jsonResult.nextExactStep = (buckets['in-progress'][0]?.nextAction) || (buckets['planned'][0]?.nextAction) || (buckets['requested'][0]?.nextAction) || null;
33
-
34
- function rowsToList(arr) {
35
- if (!arr || !arr.length) return '- 없음';
36
- return arr.map(r => `- ${r.id} ${r.request} next: ${r.nextAction}`).join('\n');
37
- }
38
-
39
- // 1.9.287 (Codex 리뷰 수렴): evidence 임베딩 시 코드펜스(```) 가 session-handoff.md 마크다운을 깨뜨리는 품질 버그 수정.
40
- const evidenceSummary = _sanitizeFences(exists(evidencePath(root)) ? (read(evidencePath(root)).split('\n').slice(-30).join('\n')) : '(no review-evidence.md)');
41
- const block = [
42
- `# Session Handoff`,
43
- ``,
44
- `Last generated: ${now()}`,
45
- ``,
46
- `## Completed`,
47
- rowsToList(buckets['done']),
48
- ``,
49
- `## In Progress`,
50
- rowsToList(buckets['in-progress']),
51
- ``,
52
- `## Incomplete / Waiting / On Hold / Blocked`,
53
- rowsToList([...(buckets['incomplete']||[]), ...(buckets['waiting']||[]), ...(buckets['on-hold']||[]), ...(buckets['blocked']||[])]),
54
- ``,
55
- `## Dropped`,
56
- rowsToList(buckets['dropped']),
57
- ``,
58
- `## Verification`,
59
- '```',
60
- evidenceSummary.trim() || '(empty)',
61
- '```',
62
- ``,
63
- `## Recommended Direction`,
64
- `- ${(buckets['in-progress'][0]?.request) || (buckets['planned'][0]?.request) || (buckets['requested'][0]?.request) || '다음 우선순위를 사용자와 정합니다.'}`,
65
- ``,
66
- `## Next Exact Step`,
67
- `- ${(buckets['in-progress'][0]?.nextAction) || (buckets['planned'][0]?.nextAction) || (buckets['requested'][0]?.nextAction) || '없음'}`,
68
- ``
69
- ].join('\n');
70
- const cur = exists(handoffPath(root)) ? read(handoffPath(root)) : '';
71
- // 1.9.316 (drift 마커 버그): 프론트매터는 파일이 '---' 시작할 때만 추출.
72
- // 이전: 본문의 '---'(수평선/구분자)을 프론트매터 종료로 오인 → 구 블록(구 'Last generated')을 보존 →
73
- // session-handoff.md 에 'Last generated' 중복 누적 → drift 가 첫(=구) 매치를 읽어 'session close 누락' 영구 오발화.
74
- let frontmatter = '';
75
- if (/^---\r?\n/.test(cur)) {
76
- const fmEnd = cur.indexOf('\n---\n', 4);
77
- if (fmEnd > 0) frontmatter = cur.slice(0, fmEnd + 5) + MARK + '\n';
78
- }
79
- writeUtf8(handoffPath(root), frontmatter + block);
80
-
81
- if (exists(currentStatePath(root))) {
82
- let cs = read(currentStatePath(root));
83
- cs = cs.replace(/Updated: \d{4}-\d{2}-\d{2}/, `Updated: ${today()}`);
84
- cs = cs.replace(/## Now\n[\s\S]*?(?=\n## Next)/, `## Now\n- ${(buckets['in-progress'][0]?.request) || '대기 중'}\n`);
85
- cs = cs.replace(/## Next\n[\s\S]*?(?=\n## Blockers)/, `## Next\n- ${(buckets['planned'][0]?.request) || (buckets['requested'][0]?.request) || '계획된 작업 없음'}\n`);
86
- cs = cs.replace(/## Blockers\n[\s\S]*$/, `## Blockers\n${(buckets['blocked']||[]).map(b=>`- ${b.id} ${b.request}`).join('\n') || '-'}\n`);
87
- writeUtf8(currentStatePath(root), cs);
88
- }
89
-
90
- append(taskLogPath(root), `\n## ${today()} session-close\n- Generated session-handoff.md and refreshed current-state.md.\n`);
91
-
92
- log('# Session Close');
93
- log('## Task Lists');
94
- for (const s of STATUSES) {
95
- log(`\n### ${s}`);
96
- log(rowsToList(buckets[s]));
97
- }
98
- // 1.9.8: 검증 자동 수행 + 보고
99
- const ruleResults = verifyRules(root);
100
- jsonResult.rules = ruleResults.map(r => ({ id: r.id, trigger: r.trigger, verified: r.verified, note: r.note }));
101
- log('\n## ⚡ User Rules verification');
102
- if (!ruleResults.length) log('- 활성 없음');
103
- else {
104
- log('| ID | Trigger | Rule | Verified | Note |');
105
- log('|---|---|---|---|---|');
106
- const ic = { pass: ' pass', pending: '⓿ pending', manual: 'ⓘ manual', baseline: '○ baseline' };
107
- for (const r of ruleResults) log(`| ${r.id} | ${r.trigger} | ${r.rule.slice(0, 40)} | ${ic[r.verified] || '?'} | ${r.note} |`);
108
- }
109
- log('\n## Required final response sections');
110
- log('- 완료 작업\n- 진행 작업\n- 미완료/예정/대기/보류/차단/드랍 작업\n- 검증 결과\n- 추천 방향\n- 다음 정확한 작업\n- 활성 룰별 검증 결과');
111
- ok(`session-handoff.md and current-state.md updated`);
112
- // 1.9.12: session close 끝에 roadmap.html 자동 갱신
113
- _autoRoadmap(root, 'session-close');
114
- // 1.9.57: --suggest 옵션 마감 skill suggest + drift check + lessons 통합 보고
115
- // 1.9.59: default 활성 — --no-suggest로 명시 비활성 가능
116
- const suggestEnabled = (has('--suggest') || (!has('--no-suggest') && process.env.LEERNESS_NO_SUGGEST !== '1'));
117
- if (suggestEnabled) {
118
- const isTty = process.stdout && process.stdout.isTTY;
119
- const cy = s => isTty ? `\x1b[36m${s}\x1b[0m` : s;
120
- const dim = s => isTty ? `\x1b[2m${s}\x1b[0m` : s;
121
- log('');
122
- log(cy('## 💡 다음 라운드 추천 (1.9.57 --suggest)'));
123
- // 1) skill suggest
124
- try {
125
- const r = cp.spawnSync(process.execPath, [harnessPath, 'skill', 'suggest', '--path', root, '--min', '3', '--json'],
126
- { encoding: 'utf8', timeout: 15000, env: { ...process.env, LEERNESS_INTERNAL: '1', LEERNESS_NO_PROMPT: '1', LEERNESS_NO_DRIFT_CHECK: '1' } });
127
- const j = JSON.parse(r.stdout);
128
- if (j.candidates && j.candidates.length) {
129
- log(dim(' 📌 신규 skill 후보 (Hermes-style 자동 학습):'));
130
- for (const c of j.candidates.slice(0, 3)) log(` • ${c.keyword} (${c.count}회 등장, 출처: ${c.source})`);
131
- jsonResult.skillCandidates = j.candidates.slice(0, 5);
132
- }
133
- } catch {}
134
- // 2) drift check
135
- try {
136
- const r = cp.spawnSync(process.execPath, [harnessPath, 'drift', 'check', root, '--json'],
137
- { encoding: 'utf8', timeout: 15000, env: { ...process.env, LEERNESS_INTERNAL: '1', LEERNESS_NO_PROMPT: '1', LEERNESS_NO_DRIFT_CHECK: '0' } });
138
- const j = JSON.parse(r.stdout.trim());
139
- if (j.level) {
140
- log(dim(` 🩺 drift 상태: ${j.level} ${j.score}/200`));
141
- if (j.fired && j.fired.length) log(dim(` 🔥 ${j.fired.length}건 임계 초과 \`leerness drift check\` 상세`));
142
- jsonResult.drift = { level: j.level, score: j.score, fired: (j.fired || []).map(f => ({ label: f.label, weight: f.weight })) };
143
- }
144
- } catch {}
145
- // 3) usage stats top
146
- try {
147
- const stats = _readUsageStats(root);
148
- const entries = Object.entries(stats.commands || {}).sort((a, b) => b[1] - a[1]).slice(0, 3);
149
- if (entries.length) {
150
- log(dim(` 📊 가장 많이 쓴 명령: ${entries.map(([c, n]) => `${c}(${n})`).join(', ')}`));
151
- jsonResult.topCommands = entries.map(([command, count]) => ({ command, count }));
152
- }
153
- // 1.9.74: MCP tools/call 통계 + rare 도구 노출
154
- if (stats.mcp && stats.mcp.tools) {
155
- const mcpEntries = Object.entries(stats.mcp.tools).sort((a, b) => b[1] - a[1]);
156
- if (mcpEntries.length) {
157
- const mcpTotal = mcpEntries.reduce((s, [, n]) => s + n, 0);
158
- log(dim(` 🔌 MCP 호출 (1.9.74): ${mcpTotal}회, top: ${mcpEntries.slice(0, 3).map(([t, n]) => `${t}(${n})`).join(', ')}`));
159
- const threshold = Math.max(1, Math.floor(mcpTotal * 0.05));
160
- const rare = mcpEntries.filter(([, n]) => n <= threshold).map(([t]) => t);
161
- if (rare.length && mcpTotal >= 5) log(dim(` 💡 드물게 호출된 MCP: ${rare.slice(0, 4).join(', ')}`));
162
- jsonResult.mcpStats = { total: mcpTotal, top: mcpEntries.slice(0, 5).map(([tool, count]) => ({ tool, count })), rare: rare.slice(0, 10) };
163
- }
164
- }
165
- } catch {}
166
- // 1.9.74: skill match query top (skill-suggestions.md 누적)
167
- try {
168
- const histPath = path.join(root, '.harness', 'skill-suggestions.md');
169
- if (exists(histPath)) {
170
- const histTxt = read(histPath);
171
- const queries = [];
172
- for (const block of histTxt.split(/\n(?=## )/)) {
173
- const h = block.match(/^## ([\d-]+ [\d:]+) — query "([^"]+)"/);
174
- if (h) queries.push(h[2]);
175
- }
176
- if (queries.length) {
177
- // 같은 query 개수 카운트
178
- const counts = {};
179
- for (const q of queries) counts[q] = (counts[q] || 0) + 1;
180
- const topQueries = Object.entries(counts).sort((a, b) => b[1] - a[1]).slice(0, 3);
181
- log(dim(` 📒 skill match query 누적 (1.9.74): 총 ${queries.length}회 / 종류 ${Object.keys(counts).length}개`));
182
- for (const [q, n] of topQueries) log(dim(` • "${q.slice(0, 50)}"${n > 1 ? ` (${n}회)` : ''}`));
183
- }
184
- }
185
- } catch {}
186
- log('');
187
- }
188
- // 1.9.13: 세션 카운터 + 자동 한 줄 요약 + 5세션마다 깊은 회고
189
- try {
190
- const sc = readSessionCounter(root);
191
- sc.count = (sc.count || 0) + 1;
192
- sc.lastCloseAt = now();
193
- writeSessionCounter(root, sc);
194
- const agg = _retroAggregate(root);
195
- log(`\n## 📈 진행 요약 (session #${sc.count})`);
196
- log(` ${_retroOneLine(agg)}`);
197
- // 1.9.132: archive 활동 1줄 요약 — 마감 시점에 DELETE 활동 가시화 (handoff 7번째 회수와 symmetric)
198
- try {
199
- const hdSC = path.join(root, '.harness');
200
- const arc = { d: 0, l: 0, p: 0, total: 0 };
201
- for (const [k, f] of [['d', 'decisions.archive.md'], ['l', 'lessons.archive.md'], ['p', 'plan.archive.md']]) {
202
- const fp = path.join(hdSC, f);
203
- if (exists(fp)) {
204
- const entries = _parseArchiveBlocks(read(fp));
205
- arc[k] = entries.length;
206
- arc.total += entries.length;
207
- }
208
- }
209
- if (arc.total > 0) {
210
- log(` 🗑 archive 누적: D${arc.d}/L${arc.l}/P${arc.p} (${arc.total}건) 복원 후보: leerness memory archive list`);
211
- }
212
- } catch {}
213
- if (sc.count % 5 === 0) {
214
- log(`\n## 🔄 ${sc.count}세션 마일스톤자동 회고 (5세션마다)`);
215
- retroCmd(root);
216
- sc.lastDeepRetroAt = now();
217
- writeSessionCounter(root, sc);
218
- } else {
219
- const left = 5 - (sc.count % 5);
220
- log(` 💡 ${left}세션 후 자동 깊은 회고 — \`leerness retro\`로 즉시 실행 가능`);
221
- }
222
- // 1.9.16: 워크스페이스 안내 (다른 leerness 프로젝트가 있으면)
223
- try {
224
- const wsCands = [path.resolve(root, '_apps'), path.resolve(root, '..', '_apps')];
225
- let wsCount = 0;
226
- for (const base of wsCands) {
227
- if (!exists(base)) continue;
228
- try { if (!fs.statSync(base).isDirectory()) continue; } catch { continue; }
229
- for (const e of fs.readdirSync(base)) {
230
- try {
231
- const p = path.join(base, e);
232
- if (fs.statSync(p).isDirectory() && exists(path.join(p, '.harness')) && p !== root) wsCount++;
233
- } catch {}
234
- }
235
- }
236
- if (wsCount > 0) log(` 🌐 워크스페이스에 ${wsCount}개 다른 leerness 프로젝트 \`leerness retro --all-apps\`로 통합 회고`);
237
- jsonResult.workspacePeers = wsCount;
238
- } catch {}
239
- } catch (e) {
240
- warn('retro 요약 실패: ' + (e && e.message ? e.message : e));
241
- jsonResult.retroSummaryError = e && e.message ? e.message : String(e);
242
- }
243
- } finally {
244
- // 1.9.103: stdout 복원
245
- if (jsonMode) process.stdout.write = _origWrite;
246
- }
247
- // 1.9.103: JSON 모드 — 구조화 출력
248
- if (jsonMode) {
249
- try {
250
- const sc = readSessionCounter(root);
251
- jsonResult.sessionNumber = sc.count;
252
- } catch {}
253
- // 1.9.122: memorySurface 통합 (handoff --json 1.9.115 와 동일 패턴)
254
- try {
255
- const rows0 = readProgressRows(root);
256
- const tasksByStatus0 = {};
257
- for (const s of STATUSES) tasksByStatus0[s] = 0;
258
- for (const r of rows0) tasksByStatus0[r.status] = (tasksByStatus0[r.status] || 0) + 1;
259
- const tasksInProgress0 = tasksByStatus0['in-progress'] || 0;
260
- const decisionsCount0 = _loadDecisions(root).length; // 1.9.339 (UR-0053): canonical 단일 진실소스
261
- const rules0 = readRules(root);
262
- const rulesActive0 = rules0.filter(r => r.status === 'active').length;
263
- const planText0 = exists(planPath(root)) ? read(planPath(root)) : '';
264
- const milestones0 = (planText0.match(/^### M-\d{4}\./gm) || []).length;
265
- const lessonsCount0 = _loadLessons(root).length;
266
- // 1.9.130: archive 카운트 통합
267
- const archiveCountsS = { decisions: 0, lessons: 0, plan: 0, total: 0 };
268
- try {
269
- const hdS = path.join(root, '.harness');
270
- for (const [key, file] of [['decisions', 'decisions.archive.md'], ['lessons', 'lessons.archive.md'], ['plan', 'plan.archive.md']]) {
271
- const fpS = path.join(hdS, file);
272
- if (exists(fpS)) {
273
- const entries = _parseArchiveBlocks(read(fpS));
274
- archiveCountsS[key] = entries.length;
275
- archiveCountsS.total += entries.length;
276
- }
277
- }
278
- } catch {}
279
- jsonResult.memorySurface = {
280
- tasks: { inProgress: tasksInProgress0, total: rows0.length, byStatus: tasksByStatus0 },
281
- decisions: { count: decisionsCount0 },
282
- rules: { active: rulesActive0, total: rules0.length },
283
- plan: { milestones: milestones0 },
284
- lessons: { count: lessonsCount0 },
285
- archive: archiveCountsS, // 1.9.130
286
- summary: `T${tasksInProgress0}/D${decisionsCount0}/R${rulesActive0}/P${milestones0}/L${lessonsCount0}`,
287
- };
288
- // 1.9.142: featureCounts 통합 session close JSON에 Feature Graph 통계
289
- try {
290
- const { nodes: fNodesC } = _readFeatureGraph(root);
291
- const edgeCount = fNodesC.reduce((s, n) => s + (n.dependsOn?.length || 0) + (n.affects?.length || 0) + (n.coChangesWith?.length || 0), 0);
292
- const linkedIds = new Set();
293
- for (const n of fNodesC) {
294
- for (const x of [...(n.dependsOn||[]), ...(n.affects||[]), ...(n.coChangesWith||[])]) { linkedIds.add(n.id); linkedIds.add(x); }
295
- }
296
- const isolated = fNodesC.length ? (fNodesC.length - linkedIds.size) : 0;
297
- jsonResult.featureGraph = {
298
- total: fNodesC.length,
299
- edges: edgeCount,
300
- isolated: Math.max(0, isolated),
301
- summary: `F${fNodesC.length}/E${edgeCount}${isolated > 0 ? `/iso${isolated}` : ''}`
302
- };
303
- } catch {}
304
- } catch {}
305
-
306
- // 1.9.217: session close 자동 통합 — 1.9.207 + 1.9.209 + 1.9.212
307
- // 마감 미답 요청 / pre-wake audit / 멱등성 검사 자동 실행 + JSON 통합
308
- try {
309
- // 1.9.207: 미답 사용자 요청 audit
310
- const reqAudit = _auditUserRequests(root);
311
- jsonResult.userRequestsAudit = {
312
- total: reqAudit.total,
313
- open: reqAudit.open,
314
- missing: reqAudit.missing ? reqAudit.missing.length : 0,
315
- tracked: reqAudit.tracked ? reqAudit.tracked.length : 0,
316
- stale: reqAudit.stale ? reqAudit.stale.length : 0
317
- };
318
- // 1.9.223: delivered 패턴 자동 감지 통합
319
- try {
320
- const delivered = _detectDeliveredRequests(root);
321
- jsonResult.deliveredRequests = {
322
- candidates: delivered.candidates.length,
323
- currentVersion: delivered.currentVersion,
324
- autoCompleteAvailable: delivered.candidates.length > 0
325
- };
326
- } catch {}
327
- // 1.9.227: roundHistory 통합 (session close JSON 6번째 통합 필드)
328
- try {
329
- const rh = _computeRoundHistory(root);
330
- jsonResult.roundHistory = {
331
- roundCount: rh.roundCount,
332
- baselineVersion: rh.baselineVersion,
333
- nextMilestone: rh.nextMilestone,
334
- roundsToNextMilestone: rh.roundsToNextMilestone,
335
- daysActive: rh.daysActive,
336
- avgRoundsPerDay: rh.avgRoundsPerDay
337
- };
338
- } catch {}
339
- // 1.9.230: milestones 통합 (session close JSON 7번째 통합 필드)
340
- try {
341
- const ms = _computeMilestones(root);
342
- jsonResult.milestones = {
343
- reachedCount: ms.reached.length,
344
- reached: ms.reached.map(m => ({ milestone: m.milestone, version: m.version, reachedAt: m.reachedAt })),
345
- next: ms.next,
346
- avgRoundsPerDay: ms.avgRoundsPerDay
347
- };
348
- } catch {}
349
- // 1.9.234: recentChanges 통합 (session close JSON 8번째 통합 필드) — 최근 5 라운드 변경
350
- try {
351
- jsonResult.recentChanges = _computeRecentChanges(root, 5);
352
- } catch {}
353
- // 1.9.240: pyFiles 통합 (session close JSON 9번째 통합 필드) — UR-0013 2단계
354
- try {
355
- const pyFiles = _collectPyFiles(root, 200);
356
- const analyses = pyFiles.slice(0, 200).map(f => _analyzePyFile(f)).filter(Boolean);
357
- jsonResult.pyFiles = {
358
- total: pyFiles.length,
359
- analyzed: analyses.length,
360
- totalLOC: analyses.reduce((s, a) => s + a.loc, 0),
361
- totalImports: analyses.reduce((s, a) => s + a.imports, 0),
362
- totalFuncs: analyses.reduce((s, a) => s + a.funcs, 0),
363
- totalClasses: analyses.reduce((s, a) => s + a.classes, 0)
364
- };
365
- } catch {}
366
- // 1.9.242: envInfo 통합 (session close JSON 10번째 통합 필드) — UR-0014 2단계
367
- try {
368
- const runtimeEnv = _collectRuntimeEnv();
369
- const encScan = _scanShellScriptsEncoding(root);
370
- jsonResult.envInfo = {
371
- os: runtimeEnv.os.platform,
372
- isKoreanWindows: runtimeEnv.locale.isKoreanWindows || false,
373
- codepage: runtimeEnv.locale.codepage || null,
374
- nodeVersion: runtimeEnv.node.version,
375
- shellScriptsScanned: encScan.scanned,
376
- encodingRiskCount: encScan.atRisk.length,
377
- encodingRiskFiles: encScan.atRisk.slice(0, 5).map(r => r.file),
378
- // 1.9.249 (UR-0018): 터미널 출력 인코딩 안전 여부 + 자동 회복 결과
379
- terminalEncodingOk: runtimeEnv.locale.codepage === 65001 || !runtimeEnv.locale.isKoreanWindows,
380
- autoChcpApplied: process.env._LEERNESS_AUTOCHCP_APPLIED || null,
381
- // 1.9.250 (UR-0018 2단계): POSIX (Linux/macOS/WSL) terminal encoding 점검
382
- posixEncodingOk: runtimeEnv.locale.posixEncodingOk,
383
- isWSL: runtimeEnv.locale.isWSL || false
384
- };
385
- } catch {}
386
- // 1.9.245: apiSkills 통합 (session close JSON 11번째 통합 필드) — UR-0015
387
- try {
388
- const allSkills = _listAPISkills(root);
389
- let currentTaskText = '';
390
- try {
391
- const rows = readProgressRows(root);
392
- const ip = rows.find(r => r.status === 'in-progress');
393
- if (ip) currentTaskText = (ip.title || '') + ' ' + (ip.notes || '');
394
- } catch {}
395
- const matched = currentTaskText ? _matchAPISkills(root, currentTaskText) : [];
396
- jsonResult.apiSkills = {
397
- total: allSkills.length,
398
- matched: matched.length,
399
- matchedIds: matched.slice(0, 5).map(s => s.id),
400
- ids: allSkills.slice(0, 10).map(s => s.id)
401
- };
402
- } catch {}
403
- // 1.9.264: shellGuard 통합 (session close JSON 12번째 통합 필드) — UR-0020 셸 실패 메모리 + 환경 변동
404
- try {
405
- const sf = _loadShellFailures(root);
406
- const drift = _shellEnvDrift(root);
407
- jsonResult.shellGuard = {
408
- failureCount: sf.failures.length,
409
- recent: sf.failures.slice(-3).map(f => ({ cmd: (f.cmd || '').slice(0, 50), exitCode: f.exitCode, shell: f.shell, rules: f.issues || [] })),
410
- envDriftChanges: drift && drift.changes ? drift.changes.length : 0,
411
- envDrift: drift ? drift.changes : null
412
- };
413
- } catch {}
414
- } catch {}
415
- try {
416
- // 1.9.209: pre-wake-audit 자동 실행 + 저장 (sleep 전 자동 점검)
417
- if (!opts.noPreWake && !has('--no-pre-wake')) {
418
- const audit = _runPreWakeAudit(root);
419
- _saveAndAppendPreWakeReport(root, audit);
420
- jsonResult.preWakeAudit = {
421
- auditedAt: audit.auditedAt,
422
- critical: audit.summary.criticalCount,
423
- warning: audit.summary.warningCount,
424
- info: audit.summary.infoCount,
425
- needsAttention: audit.summary.needsAttention
426
- };
427
- }
428
- } catch {}
429
- try {
430
- // 1.9.212: 멱등성 검사 자동 실행 (rule/task/user-requests/wakeups 4영역)
431
- const idemp = _runIdempotencyAudit(root);
432
- jsonResult.idempotencyAudit = {
433
- violations: idemp.summary.totalViolations,
434
- high: idemp.summary.highSeverity,
435
- medium: idemp.summary.mediumSeverity,
436
- low: idemp.summary.lowSeverity,
437
- verified: idemp.summary.verifiedAreas,
438
- overall: idemp.summary.overall
439
- };
440
- } catch {}
441
- try {
442
- // 1.9.221: abnormalShutdown 자동 감지 (1.9.220 통합) — session close 시 다음 재개 가이드 회수
443
- const ad = _detectAbnormalShutdown(root);
444
- jsonResult.abnormalShutdown = {
445
- detected: ad.abnormalShutdown,
446
- severity: ad.severity,
447
- signalCount: ad.signals.length,
448
- signals: ad.signals.map(s => ({ kind: s.kind, severity: s.severity, detail: s.detail })),
449
- resumeGuide: ad.resumeGuide
450
- };
451
- } catch {}
452
-
453
- process.stdout.write(JSON.stringify(jsonResult, null, 2) + '\n');
454
- } else {
455
- // 1.9.217: human 출력 모드에서도 통합 보고 노출 (마감 직전)
456
- try {
457
- const isTty = process.stdout.isTTY;
458
- const grn = s => isTty ? `\x1b[32m${s}\x1b[0m` : s;
459
- const yel = s => isTty ? `\x1b[33m${s}\x1b[0m` : s;
460
- const red = s => isTty ? `\x1b[31m${s}\x1b[0m` : s;
461
- const dim = s => isTty ? `\x1b[2m${s}\x1b[0m` : s;
462
-
463
- log('');
464
- log(`## 🔚 session close 자동 통합 보고 (1.9.217)`);
465
- // 1.9.207 + 1.9.223 (delivered 패턴 자동 권장) + 1.9.224 (--auto-apply-delivered 옵션)
466
- try {
467
- const reqAudit = _auditUserRequests(root);
468
- const missCnt = reqAudit.missing ? reqAudit.missing.length : 0;
469
- let delivered = { candidates: [] };
470
- try { delivered = _detectDeliveredRequests(root); } catch {}
471
- if (delivered.candidates && delivered.candidates.length > 0) {
472
- if (has('--auto-apply-delivered')) {
473
- // 1.9.224: 자동 정리 (마감 호출 — 안전: 패턴 매칭 + 버전 가드)
474
- let ok = 0;
475
- for (const c of delivered.candidates) {
476
- const u = _updateUserRequest(root, c.id, { status: 'completed', autoCompletedAt: new Date().toISOString(), autoCompleteReason: 'session-close-auto-apply-1.9.224' });
477
- if (u) ok++;
478
- }
479
- log(grn(` ✓ delivered 패턴 ${ok}건 자동 완료 (--auto-apply-delivered 1.9.224)`));
480
- } else {
481
- log(yel(` 📥 delivered 패턴 ${delivered.candidates.length}건 (1.9.223) — 자동 완료 가능`));
482
- log(dim(` → leerness requests auto-complete --apply (수동) 또는 session close --auto-apply-delivered (1.9.224)`));
483
- }
484
- } else if (missCnt > 0) {
485
- log(red(` 미답 사용자 요청 ${missCnt}건 (task-log/plan/decisions 매칭 )`));
486
- } else if (reqAudit.open > 0) {
487
- log(grn(` ✓ 사용자 요청 ${reqAudit.open}건 모두 tracked`));
488
- } else {
489
- log(dim(` 사용자 요청 없음 (UR 백로그 비어있음)`));
490
- }
491
- } catch {}
492
- // 1.9.209
493
- try {
494
- if (!opts.noPreWake && !has('--no-pre-wake')) {
495
- const audit = _runPreWakeAudit(root);
496
- _saveAndAppendPreWakeReport(root, audit);
497
- const sum = audit.summary;
498
- if (sum.criticalCount > 0) {
499
- log(red(` 🚨 pre-wake-audit: critical ${sum.criticalCount} (다음 깨어남 시 점검 필요)`));
500
- } else if (sum.warningCount > 0) {
501
- log(yel(` ⚠ pre-wake-audit: warning ${sum.warningCount}`));
502
- } else {
503
- log(grn(` pre-wake-audit: clean (sleep 안전)`));
504
- }
505
- }
506
- } catch {}
507
- // 1.9.212
508
- try {
509
- const idemp = _runIdempotencyAudit(root);
510
- const v = idemp.summary.totalViolations;
511
- if (v > 0) {
512
- log(red(` ⚠ 멱등성 위반 ${v}건 (high: ${idemp.summary.highSeverity})`));
513
- log(dim(` → leerness idempotency audit 으로 상세 확인`));
514
- } else {
515
- log(grn(` ✓ 멱등성 검사 통과 — verified ${idemp.summary.verifiedAreas} 영역`));
516
- }
517
- } catch {}
518
- // 1.9.264: 셸 실패 메모리 + 환경 변동 요약 (UR-0020) — 마감 시 이번 세션 셸 실패를 회고에 노출
519
- try {
520
- const sf = _loadShellFailures(root);
521
- const drift = _shellEnvDrift(root);
522
- const driftN = drift && drift.changes ? drift.changes.length : 0;
523
- if (sf.failures.length > 0 || driftN > 0) {
524
- if (driftN > 0) log(yel(` ⚠ 환경 버전 변동 ${driftN}건 — 다음 세션 셸 실패 기록 재검토 권장`));
525
- if (sf.failures.length > 0) {
526
- log(yel(` 🐚 실패 누적 ${sf.failures.length}건 다음 handoff 가 자동 노출`));
527
- log(dim(` → 명령 실행 점검: leerness shell-guard "<command>"`));
528
- }
529
- } else {
530
- log(grn(` 셸 실패 기록 없음 (터미널 호환성 양호)`));
531
- }
532
- } catch {}
533
- // 1.9.237: session close --auto-cleanup-branches — 50+ release/* branches 시 자동 정리
534
- // 1.9.224 패턴 (--auto-apply-delivered) 확장 마감 운영 누적 폐기물 자동 정리
535
- // 안전: keep 10, merged 만, 현재 branch 보호
536
- try {
537
- const branchR = cp.spawnSync('git', ['branch', '--merged', 'main', '--list', 'release/*'], { cwd: root, encoding: 'utf8' });
538
- if (branchR.status === 0) {
539
- const merged = (branchR.stdout || '').split('\n')
540
- .map(l => l.replace(/^\*?\s+/, '').trim())
541
- .filter(l => l && /^release\/\d+\.\d+\.\d+$/.test(l));
542
- if (merged.length > 50) {
543
- if (has('--auto-cleanup-branches')) {
544
- merged.sort((a, b) => {
545
- const va = a.replace('release/', '').split('.').map(n => parseInt(n, 10) || 0);
546
- const vb = b.replace('release/', '').split('.').map(n => parseInt(n, 10) || 0);
547
- for (let i = 0; i < 3; i++) if (va[i] !== vb[i]) return vb[i] - va[i];
548
- return 0;
549
- });
550
- const curR = cp.spawnSync('git', ['rev-parse', '--abbrev-ref', 'HEAD'], { cwd: root, encoding: 'utf8' });
551
- const cur = (curR.stdout || '').trim();
552
- const toDelete = merged.slice(10).filter(b => b !== cur);
553
- let okCnt = 0;
554
- for (const b of toDelete) {
555
- const r = cp.spawnSync('git', ['branch', '-d', b], { cwd: root, encoding: 'utf8' });
556
- if (r.status === 0) okCnt++;
557
- }
558
- log(grn(` ✓ release 정리 ${okCnt}/${toDelete.length}건 (--auto-cleanup-branches 1.9.237, keep 10)`));
559
- } else {
560
- log(yel(` 🗑 release/* merged ${merged.length}개 (50+) — cleanup 가능 (1.9.235)`));
561
- log(dim(` → leerness release cleanup --apply --keep 10 (수동)`));
562
- log(dim(` 또는 session close --auto-cleanup-branches (1.9.237 자동)`));
563
- }
564
- }
565
- }
566
- } catch {}
567
- // 1.9.243: session close --auto-fix-encoding — 셸 스크립트 인코딩 위험 자동 회복 (UR-0014 3단계)
568
- // 1.9.224 (--auto-apply-delivered) / 1.9.237 (--auto-cleanup-branches) 패턴 확장
569
- // 마감 시 한국어/일본어/중국어 PowerShell 인코딩 위험 자동 BOM 추가
570
- try {
571
- const encScan = _scanShellScriptsEncoding(root);
572
- if (encScan.atRisk && encScan.atRisk.length > 0) {
573
- if (has('--auto-fix-encoding')) {
574
- let ok = 0;
575
- for (const r of encScan.atRisk) {
576
- try {
577
- const fullPath = path.join(root, r.file);
578
- const orig = fs.readFileSync(fullPath);
579
- const bom = Buffer.from([0xEF, 0xBB, 0xBF]);
580
- const fixed = Buffer.concat([bom, orig]);
581
- fs.writeFileSync(fullPath, fixed);
582
- ok++;
583
- } catch {}
584
- }
585
- log(grn(` ✓ 인코딩 위험 ${ok}/${encScan.atRisk.length}건 UTF-8 BOM 자동 추가 (--auto-fix-encoding 1.9.243)`));
586
- } else {
587
- log(yel(` ⚠ 스크립트 인코딩 위험 ${encScan.atRisk.length}건 (1.9.241) — 자동 회복 가능`));
588
- log(dim(` → leerness env encoding --apply (수동) 또는 session close --auto-fix-encoding (1.9.243 자동)`));
589
- }
590
- }
591
- } catch {}
592
- // 1.9.232: 마감 pulse 자동 노출 다음 라운드 진입 시 즉시 상태 인지
593
- try {
594
- const rh = _computeRoundHistory(root);
595
- const ms = _computeMilestones(root);
596
- const rows = readProgressRows(root);
597
- const tIn = rows.filter(r => r.status === 'in-progress').length;
598
- const dCnt = _loadDecisions(root).length; // 1.9.339 (UR-0053): canonical 단일 진실소스
599
- const rActive = readRules(root).filter(r => r.status === 'active').length;
600
- const planText = exists(planPath(root)) ? read(planPath(root)) : '';
601
- const pCnt = (planText.match(/^### M-\d{4}\./gm) || []).length;
602
- const lCnt = _loadLessons(root).length;
603
- const mem = `T${tIn}/D${dCnt}/R${rActive}/P${pCnt}/L${lCnt}`;
604
- let pulseLine = `📍 v${VERSION} · 🔄 R${rh.roundCount} · 🧠 ${mem}`;
605
- if (ms.next) {
606
- const eta = ms.next.etaDays != null ? ` (${ms.next.etaDays}d)` : '';
607
- pulseLine += ` · 🎯 R${ms.next.milestone}${eta}`;
608
- }
609
- log('');
610
- log(` ${pulseLine} ${dim('— leerness pulse (1.9.232)')}`);
611
- } catch {}
612
- } catch {}
613
- }
614
- }
615
-
616
- module.exports = { sessionClose };
1
+ // lib/session-close.js — session close 핸들러 (UR-0025/UR-0125 큰 핸들러 모듈화 10번째, 1.9.425)
2
+ // bin/leerness.js 에서 sessionClose(599줄) 분리. DI: harness 고유 의존 다수 주입.
3
+ // io 프리미티브는 ./io, _sanitizeFences/_parseArchiveBlocks 는 ./pure-utils, cp/os/path/fs 빌트인.
4
+ // __filename→harnessPath(self-spawn). 동작/출력 무변경(9 카테고리 + 활성 룰 검증 + retro 등).
5
+ 'use strict';
6
+ const cp = require('child_process');
7
+ const os = require('os');
8
+ const path = require('path');
9
+ const fs = require('fs');
10
+ const { log, ok, warn, fail, failJson, today, now, absRoot, exists, read, readBuf, mkdirp, writeUtf8, append, rel } = require('./io');
11
+ const { _sanitizeFences, _parseArchiveBlocks } = require('./pure-utils');
12
+
13
+ function sessionClose(root, opts = {}, deps = {}) {
14
+ const { VERSION, STATUSES, MARK, has, arg, harnessPath, readProgressRows, evidencePath, handoffPath, currentStatePath, taskLogPath, verifyRules, _autoRoadmap, _readUsageStats, readSessionCounter, writeSessionCounter, _retroAggregate, _retroOneLine, retroCmd, _loadDecisions, readRules, planPath, _loadLessons, _readFeatureGraph, _auditUserRequests, _detectDeliveredRequests, _computeRoundHistory, _computeMilestones, _computeRecentChanges, _collectPyFiles, _analyzePyFile, _collectRuntimeEnv, _scanShellScriptsEncoding, _listAPISkills, _matchAPISkills, _loadShellFailures, _shellEnvDrift, _runPreWakeAudit, _saveAndAppendPreWakeReport, _runIdempotencyAudit, _detectAbnormalShutdown, _updateUserRequest } = deps;
15
+ root = absRoot(root);
16
+ // 1.10.4 (13th 버그헌트 P2, UR-0167): 경로 없음/디렉토리 아님 → 구조화 에러 + exit 1. mkdir <path>/.harness ENOTDIR 크래시 & 실패를 성공(exit 0)으로 오판하던 문제 차단.
17
+ if (!exists(root) || !fs.statSync(root).isDirectory()) { failJson(!!opts.json || has('--json'), 'path_not_found', `경로 없음 또는 디렉토리 아님: ${root}`); return; }
18
+ // 1.9.103: --json 모드 — stdout 억제 후 구조화 출력
19
+ const jsonMode = !!opts.json || has('--json');
20
+ const _origWrite = process.stdout.write.bind(process.stdout);
21
+ if (jsonMode) process.stdout.write = () => true;
22
+ const jsonResult = { version: VERSION, root, closedAt: now() };
23
+ try {
24
+ const rows = readProgressRows(root);
25
+ const buckets = {};
26
+ for (const s of STATUSES) buckets[s] = [];
27
+ for (const r of rows) (buckets[r.status] || (buckets[r.status] = [])).push(r);
28
+ // 1.9.103: JSON 결과 누적
29
+ jsonResult.taskCounts = {};
30
+ for (const s of STATUSES) jsonResult.taskCounts[s] = (buckets[s] || []).length;
31
+ jsonResult.recommendedDirection = (buckets['in-progress'][0]?.request) || (buckets['planned'][0]?.request) || (buckets['requested'][0]?.request) || null;
32
+ jsonResult.nextExactStep = (buckets['in-progress'][0]?.nextAction) || (buckets['planned'][0]?.nextAction) || (buckets['requested'][0]?.nextAction) || null;
33
+ // 1.12.3 (14th 버그헌트 P3, UR-0183): 마감 시 완료 정직성 advisory — done 인데 evidence 가 비었거나 placeholder 인 task 노출(차단 X, 정직성 환기). lazy detect 의 done_no_evidence 휴리스틱과 동일.
34
+ const _doneNoEvidence = (buckets['done'] || []).filter(r => !r.evidence || /^(\s*|user-request|-)$/.test(r.evidence) || /^plan:M-\d{4}\s*$/.test(r.evidence));
35
+ jsonResult.completionHonesty = { doneTotal: (buckets['done'] || []).length, doneWithoutEvidence: _doneNoEvidence.length, ids: _doneNoEvidence.slice(0, 5).map(r => r.id) };
36
+ if (_doneNoEvidence.length) log(` ⚠ 완료 정직성: done ${_doneNoEvidence.length} evidence 없음/placeholder (${_doneNoEvidence.slice(0, 3).map(r => r.id).join(', ')}) — verify-claim 권장 (advisory)`);
37
+
38
+ function rowsToList(arr) {
39
+ if (!arr || !arr.length) return '- 없음';
40
+ return arr.map(r => `- ${r.id} ${r.request} → next: ${r.nextAction}`).join('\n');
41
+ }
42
+
43
+ // 1.9.287 (Codex 리뷰 수렴): evidence 임베딩 시 코드펜스(```) 가 session-handoff.md 마크다운을 깨뜨리는 품질 버그 수정.
44
+ const evidenceSummary = _sanitizeFences(exists(evidencePath(root)) ? (read(evidencePath(root)).split('\n').slice(-30).join('\n')) : '(no review-evidence.md)');
45
+ const block = [
46
+ `# Session Handoff`,
47
+ ``,
48
+ `Last generated: ${now()}`,
49
+ ``,
50
+ `## Completed`,
51
+ rowsToList(buckets['done']),
52
+ ``,
53
+ `## In Progress`,
54
+ rowsToList(buckets['in-progress']),
55
+ ``,
56
+ `## Incomplete / Waiting / On Hold / Blocked`,
57
+ rowsToList([...(buckets['incomplete']||[]), ...(buckets['waiting']||[]), ...(buckets['on-hold']||[]), ...(buckets['blocked']||[])]),
58
+ ``,
59
+ `## Dropped`,
60
+ rowsToList(buckets['dropped']),
61
+ ``,
62
+ `## Verification`,
63
+ '```',
64
+ evidenceSummary.trim() || '(empty)',
65
+ '```',
66
+ ``,
67
+ `## Recommended Direction`,
68
+ `- ${(buckets['in-progress'][0]?.request) || (buckets['planned'][0]?.request) || (buckets['requested'][0]?.request) || '다음 우선순위를 사용자와 정합니다.'}`,
69
+ ``,
70
+ `## Next Exact Step`,
71
+ `- ${(buckets['in-progress'][0]?.nextAction) || (buckets['planned'][0]?.nextAction) || (buckets['requested'][0]?.nextAction) || '없음'}`,
72
+ ``
73
+ ].join('\n');
74
+ const cur = exists(handoffPath(root)) ? read(handoffPath(root)) : '';
75
+ // 1.9.316 (drift 마커 버그): 프론트매터는 파일이 '---' 로 시작할 때만 추출.
76
+ // 이전: 본문의 '---'(수평선/구분자)을 프론트매터 종료로 오인 → 구 블록('Last generated')을 보존 →
77
+ // session-handoff.md 'Last generated' 중복 누적 drift 첫(=구) 매치를 읽어 'session close 누락' 영구 오발화.
78
+ let frontmatter = '';
79
+ if (/^---\r?\n/.test(cur)) {
80
+ const fmEnd = cur.indexOf('\n---\n', 4);
81
+ if (fmEnd > 0) frontmatter = cur.slice(0, fmEnd + 5) + MARK + '\n';
82
+ }
83
+ writeUtf8(handoffPath(root), frontmatter + block);
84
+
85
+ if (exists(currentStatePath(root))) {
86
+ let cs = read(currentStatePath(root));
87
+ cs = cs.replace(/Updated: \d{4}-\d{2}-\d{2}/, `Updated: ${today()}`);
88
+ cs = cs.replace(/## Now\n[\s\S]*?(?=\n## Next)/, `## Now\n- ${(buckets['in-progress'][0]?.request) || '대기 중'}\n`);
89
+ cs = cs.replace(/## Next\n[\s\S]*?(?=\n## Blockers)/, `## Next\n- ${(buckets['planned'][0]?.request) || (buckets['requested'][0]?.request) || '계획된 작업 없음'}\n`);
90
+ cs = cs.replace(/## Blockers\n[\s\S]*$/, `## Blockers\n${(buckets['blocked']||[]).map(b=>`- ${b.id} ${b.request}`).join('\n') || '-'}\n`);
91
+ writeUtf8(currentStatePath(root), cs);
92
+ }
93
+
94
+ append(taskLogPath(root), `\n## ${today()} session-close\n- Generated session-handoff.md and refreshed current-state.md.\n`);
95
+
96
+ log('# Session Close');
97
+ log('## Task Lists');
98
+ for (const s of STATUSES) {
99
+ log(`\n### ${s}`);
100
+ log(rowsToList(buckets[s]));
101
+ }
102
+ // 1.9.8: 검증 자동 수행 + 보고
103
+ const ruleResults = verifyRules(root);
104
+ jsonResult.rules = ruleResults.map(r => ({ id: r.id, trigger: r.trigger, verified: r.verified, note: r.note }));
105
+ log('\n## ⚡ User Rules verification');
106
+ if (!ruleResults.length) log('- 활성 없음');
107
+ else {
108
+ log('| ID | Trigger | Rule | Verified | Note |');
109
+ log('|---|---|---|---|---|');
110
+ const ic = { pass: '✓ pass', pending: '⓿ pending', manual: 'ⓘ manual', baseline: '○ baseline' };
111
+ for (const r of ruleResults) log(`| ${r.id} | ${r.trigger} | ${r.rule.slice(0, 40)} | ${ic[r.verified] || '?'} | ${r.note} |`);
112
+ }
113
+ log('\n## Required final response sections');
114
+ log('- 완료 작업\n- 진행 작업\n- 미완료/예정/대기/보류/차단/드랍 작업\n- 검증 결과\n- 추천 방향\n- 다음 정확한 작업\n- ⚡ 활성 룰별 검증 결과');
115
+ ok(`session-handoff.md and current-state.md updated`);
116
+ // 1.9.12: session close 끝에 roadmap.html 자동 갱신
117
+ _autoRoadmap(root, 'session-close');
118
+ // 1.9.57: --suggest 옵션 마감 시 skill suggest + drift check + lessons 통합 보고
119
+ // 1.9.59: default 활성 --no-suggest로 명시 비활성 가능
120
+ const suggestEnabled = (has('--suggest') || (!has('--no-suggest') && process.env.LEERNESS_NO_SUGGEST !== '1'));
121
+ if (suggestEnabled) {
122
+ const isTty = process.stdout && process.stdout.isTTY;
123
+ const cy = s => isTty ? `\x1b[36m${s}\x1b[0m` : s;
124
+ const dim = s => isTty ? `\x1b[2m${s}\x1b[0m` : s;
125
+ log('');
126
+ log(cy('## 💡 다음 라운드 추천 (1.9.57 --suggest)'));
127
+ // 1) skill suggest
128
+ try {
129
+ const r = cp.spawnSync(process.execPath, [harnessPath, 'skill', 'suggest', '--path', root, '--min', '3', '--json'],
130
+ { encoding: 'utf8', timeout: 15000, env: { ...process.env, LEERNESS_INTERNAL: '1', LEERNESS_NO_PROMPT: '1', LEERNESS_NO_DRIFT_CHECK: '1' } });
131
+ const j = JSON.parse(r.stdout);
132
+ if (j.candidates && j.candidates.length) {
133
+ log(dim(' 📌 신규 skill 후보 (Hermes-style 자동 학습):'));
134
+ for (const c of j.candidates.slice(0, 3)) log(` • ${c.keyword} (${c.count}회 등장, 출처: ${c.source})`);
135
+ jsonResult.skillCandidates = j.candidates.slice(0, 5);
136
+ }
137
+ } catch {}
138
+ // 2) drift check
139
+ try {
140
+ const r = cp.spawnSync(process.execPath, [harnessPath, 'drift', 'check', root, '--json'],
141
+ { encoding: 'utf8', timeout: 15000, env: { ...process.env, LEERNESS_INTERNAL: '1', LEERNESS_NO_PROMPT: '1', LEERNESS_NO_DRIFT_CHECK: '0' } });
142
+ const j = JSON.parse(r.stdout.trim());
143
+ if (j.level) {
144
+ log(dim(` 🩺 drift 상태: ${j.level} ${j.score}/200`));
145
+ if (j.fired && j.fired.length) log(dim(` 🔥 ${j.fired.length}건 임계 초과 — \`leerness drift check\` 상세`));
146
+ jsonResult.drift = { level: j.level, score: j.score, fired: (j.fired || []).map(f => ({ label: f.label, weight: f.weight })) };
147
+ }
148
+ } catch {}
149
+ // 3) usage stats top
150
+ try {
151
+ const stats = _readUsageStats(root);
152
+ const entries = Object.entries(stats.commands || {}).sort((a, b) => b[1] - a[1]).slice(0, 3);
153
+ if (entries.length) {
154
+ log(dim(` 📊 가장 많이 쓴 명령: ${entries.map(([c, n]) => `${c}(${n})`).join(', ')}`));
155
+ jsonResult.topCommands = entries.map(([command, count]) => ({ command, count }));
156
+ }
157
+ // 1.9.74: MCP tools/call 통계 + rare 도구 노출
158
+ if (stats.mcp && stats.mcp.tools) {
159
+ const mcpEntries = Object.entries(stats.mcp.tools).sort((a, b) => b[1] - a[1]);
160
+ if (mcpEntries.length) {
161
+ const mcpTotal = mcpEntries.reduce((s, [, n]) => s + n, 0);
162
+ log(dim(` 🔌 MCP 호출 (1.9.74): 총 ${mcpTotal}회, top: ${mcpEntries.slice(0, 3).map(([t, n]) => `${t}(${n})`).join(', ')}`));
163
+ const threshold = Math.max(1, Math.floor(mcpTotal * 0.05));
164
+ const rare = mcpEntries.filter(([, n]) => n <= threshold).map(([t]) => t);
165
+ if (rare.length && mcpTotal >= 5) log(dim(` 💡 드물게 호출된 MCP: ${rare.slice(0, 4).join(', ')}`));
166
+ jsonResult.mcpStats = { total: mcpTotal, top: mcpEntries.slice(0, 5).map(([tool, count]) => ({ tool, count })), rare: rare.slice(0, 10) };
167
+ }
168
+ }
169
+ } catch {}
170
+ // 1.9.74: skill match query top (skill-suggestions.md 누적)
171
+ try {
172
+ const histPath = path.join(root, '.harness', 'skill-suggestions.md');
173
+ if (exists(histPath)) {
174
+ const histTxt = read(histPath);
175
+ const queries = [];
176
+ for (const block of histTxt.split(/\n(?=## )/)) {
177
+ const h = block.match(/^## ([\d-]+ [\d:]+) — query "([^"]+)"/);
178
+ if (h) queries.push(h[2]);
179
+ }
180
+ if (queries.length) {
181
+ // 같은 query 개수 카운트
182
+ const counts = {};
183
+ for (const q of queries) counts[q] = (counts[q] || 0) + 1;
184
+ const topQueries = Object.entries(counts).sort((a, b) => b[1] - a[1]).slice(0, 3);
185
+ log(dim(` 📒 skill match query 누적 (1.9.74): 총 ${queries.length} / 종류 ${Object.keys(counts).length}개`));
186
+ for (const [q, n] of topQueries) log(dim(` • "${q.slice(0, 50)}"${n > 1 ? ` (${n}회)` : ''}`));
187
+ }
188
+ }
189
+ } catch {}
190
+ log('');
191
+ }
192
+ // 1.9.13: 세션 카운터 + 자동 한 줄 요약 + 5세션마다 깊은 회고
193
+ try {
194
+ const sc = readSessionCounter(root);
195
+ sc.count = (sc.count || 0) + 1;
196
+ sc.lastCloseAt = now();
197
+ writeSessionCounter(root, sc);
198
+ const agg = _retroAggregate(root);
199
+ log(`\n## 📈 진행 요약 (session #${sc.count})`);
200
+ log(` ${_retroOneLine(agg)}`);
201
+ // 1.9.132: archive 활동 1줄 요약 마감 시점에 DELETE 활동 가시화 (handoff 7번째 회수와 symmetric)
202
+ try {
203
+ const hdSC = path.join(root, '.harness');
204
+ const arc = { d: 0, l: 0, p: 0, total: 0 };
205
+ for (const [k, f] of [['d', 'decisions.archive.md'], ['l', 'lessons.archive.md'], ['p', 'plan.archive.md']]) {
206
+ const fp = path.join(hdSC, f);
207
+ if (exists(fp)) {
208
+ const entries = _parseArchiveBlocks(read(fp));
209
+ arc[k] = entries.length;
210
+ arc.total += entries.length;
211
+ }
212
+ }
213
+ if (arc.total > 0) {
214
+ log(` 🗑 archive 누적: D${arc.d}/L${arc.l}/P${arc.p} (${arc.total}건)복원 후보: leerness memory archive list`);
215
+ }
216
+ } catch {}
217
+ if (sc.count % 5 === 0) {
218
+ log(`\n## 🔄 ${sc.count}세션 마일스톤 — 자동 회고 (5세션마다)`);
219
+ retroCmd(root);
220
+ sc.lastDeepRetroAt = now();
221
+ writeSessionCounter(root, sc);
222
+ } else {
223
+ const left = 5 - (sc.count % 5);
224
+ log(` 💡 ${left}세션 자동 깊은 회고 \`leerness retro\`로 즉시 실행 가능`);
225
+ }
226
+ // 1.9.16: 워크스페이스 안내 (다른 leerness 프로젝트가 있으면)
227
+ try {
228
+ const wsCands = [path.resolve(root, '_apps'), path.resolve(root, '..', '_apps')];
229
+ let wsCount = 0;
230
+ for (const base of wsCands) {
231
+ if (!exists(base)) continue;
232
+ try { if (!fs.statSync(base).isDirectory()) continue; } catch { continue; }
233
+ for (const e of fs.readdirSync(base)) {
234
+ try {
235
+ const p = path.join(base, e);
236
+ if (fs.statSync(p).isDirectory() && exists(path.join(p, '.harness')) && p !== root) wsCount++;
237
+ } catch {}
238
+ }
239
+ }
240
+ if (wsCount > 0) log(` 🌐 워크스페이스에 ${wsCount}개 다른 leerness 프로젝트 \`leerness retro --all-apps\`로 통합 회고`);
241
+ jsonResult.workspacePeers = wsCount;
242
+ } catch {}
243
+ } catch (e) {
244
+ warn('retro 요약 실패: ' + (e && e.message ? e.message : e));
245
+ jsonResult.retroSummaryError = e && e.message ? e.message : String(e);
246
+ }
247
+ } finally {
248
+ // 1.9.103: stdout 복원
249
+ if (jsonMode) process.stdout.write = _origWrite;
250
+ }
251
+ // 1.9.103: JSON 모드 — 구조화 출력
252
+ if (jsonMode) {
253
+ try {
254
+ const sc = readSessionCounter(root);
255
+ jsonResult.sessionNumber = sc.count;
256
+ } catch {}
257
+ // 1.9.122: memorySurface 통합 (handoff --json 1.9.115 동일 패턴)
258
+ try {
259
+ const rows0 = readProgressRows(root);
260
+ const tasksByStatus0 = {};
261
+ for (const s of STATUSES) tasksByStatus0[s] = 0;
262
+ for (const r of rows0) tasksByStatus0[r.status] = (tasksByStatus0[r.status] || 0) + 1;
263
+ const tasksInProgress0 = tasksByStatus0['in-progress'] || 0;
264
+ const decisionsCount0 = _loadDecisions(root).length; // 1.9.339 (UR-0053): canonical 단일 진실소스
265
+ const rules0 = readRules(root);
266
+ const rulesActive0 = rules0.filter(r => r.status === 'active').length;
267
+ const planText0 = exists(planPath(root)) ? read(planPath(root)) : '';
268
+ const milestones0 = (planText0.match(/^### M-\d{4}\./gm) || []).length;
269
+ const lessonsCount0 = _loadLessons(root).length;
270
+ // 1.9.130: archive 카운트 통합
271
+ const archiveCountsS = { decisions: 0, lessons: 0, plan: 0, total: 0 };
272
+ try {
273
+ const hdS = path.join(root, '.harness');
274
+ for (const [key, file] of [['decisions', 'decisions.archive.md'], ['lessons', 'lessons.archive.md'], ['plan', 'plan.archive.md']]) {
275
+ const fpS = path.join(hdS, file);
276
+ if (exists(fpS)) {
277
+ const entries = _parseArchiveBlocks(read(fpS));
278
+ archiveCountsS[key] = entries.length;
279
+ archiveCountsS.total += entries.length;
280
+ }
281
+ }
282
+ } catch {}
283
+ jsonResult.memorySurface = {
284
+ tasks: { inProgress: tasksInProgress0, total: rows0.length, byStatus: tasksByStatus0 },
285
+ decisions: { count: decisionsCount0 },
286
+ rules: { active: rulesActive0, total: rules0.length },
287
+ plan: { milestones: milestones0 },
288
+ lessons: { count: lessonsCount0 },
289
+ archive: archiveCountsS, // 1.9.130
290
+ summary: `T${tasksInProgress0}/D${decisionsCount0}/R${rulesActive0}/P${milestones0}/L${lessonsCount0}`,
291
+ };
292
+ // 1.9.142: featureCounts 통합 — session close JSON에 Feature Graph 통계
293
+ try {
294
+ const { nodes: fNodesC } = _readFeatureGraph(root);
295
+ const edgeCount = fNodesC.reduce((s, n) => s + (n.dependsOn?.length || 0) + (n.affects?.length || 0) + (n.coChangesWith?.length || 0), 0);
296
+ const linkedIds = new Set();
297
+ for (const n of fNodesC) {
298
+ for (const x of [...(n.dependsOn||[]), ...(n.affects||[]), ...(n.coChangesWith||[])]) { linkedIds.add(n.id); linkedIds.add(x); }
299
+ }
300
+ const isolated = fNodesC.length ? (fNodesC.length - linkedIds.size) : 0;
301
+ jsonResult.featureGraph = {
302
+ total: fNodesC.length,
303
+ edges: edgeCount,
304
+ isolated: Math.max(0, isolated),
305
+ summary: `F${fNodesC.length}/E${edgeCount}${isolated > 0 ? `/iso${isolated}` : ''}`
306
+ };
307
+ } catch {}
308
+ } catch {}
309
+
310
+ // 1.9.217: session close 자동 통합 — 1.9.207 + 1.9.209 + 1.9.212
311
+ // 마감 미답 요청 / pre-wake audit / 멱등성 검사 자동 실행 + JSON 통합
312
+ try {
313
+ // 1.9.207: 미답 사용자 요청 audit
314
+ const reqAudit = _auditUserRequests(root);
315
+ jsonResult.userRequestsAudit = {
316
+ total: reqAudit.total,
317
+ open: reqAudit.open,
318
+ missing: reqAudit.missing ? reqAudit.missing.length : 0,
319
+ tracked: reqAudit.tracked ? reqAudit.tracked.length : 0,
320
+ stale: reqAudit.stale ? reqAudit.stale.length : 0
321
+ };
322
+ // 1.9.223: delivered 패턴 자동 감지 통합
323
+ try {
324
+ const delivered = _detectDeliveredRequests(root);
325
+ jsonResult.deliveredRequests = {
326
+ candidates: delivered.candidates.length,
327
+ currentVersion: delivered.currentVersion,
328
+ autoCompleteAvailable: delivered.candidates.length > 0
329
+ };
330
+ } catch {}
331
+ // 1.9.227: roundHistory 통합 (session close JSON 6번째 통합 필드)
332
+ try {
333
+ const rh = _computeRoundHistory(root);
334
+ jsonResult.roundHistory = {
335
+ roundCount: rh.roundCount,
336
+ baselineVersion: rh.baselineVersion,
337
+ nextMilestone: rh.nextMilestone,
338
+ roundsToNextMilestone: rh.roundsToNextMilestone,
339
+ daysActive: rh.daysActive,
340
+ avgRoundsPerDay: rh.avgRoundsPerDay
341
+ };
342
+ } catch {}
343
+ // 1.9.230: milestones 통합 (session close JSON 7번째 통합 필드)
344
+ try {
345
+ const ms = _computeMilestones(root);
346
+ jsonResult.milestones = {
347
+ reachedCount: ms.reached.length,
348
+ reached: ms.reached.map(m => ({ milestone: m.milestone, version: m.version, reachedAt: m.reachedAt })),
349
+ next: ms.next,
350
+ avgRoundsPerDay: ms.avgRoundsPerDay
351
+ };
352
+ } catch {}
353
+ // 1.9.234: recentChanges 통합 (session close JSON 8번째 통합 필드) — 최근 5 라운드 변경
354
+ try {
355
+ jsonResult.recentChanges = _computeRecentChanges(root, 5);
356
+ } catch {}
357
+ // 1.9.240: pyFiles 통합 (session close JSON 9번째 통합 필드) — UR-0013 2단계
358
+ try {
359
+ const pyFiles = _collectPyFiles(root, 200);
360
+ const analyses = pyFiles.slice(0, 200).map(f => _analyzePyFile(f)).filter(Boolean);
361
+ jsonResult.pyFiles = {
362
+ total: pyFiles.length,
363
+ analyzed: analyses.length,
364
+ totalLOC: analyses.reduce((s, a) => s + a.loc, 0),
365
+ totalImports: analyses.reduce((s, a) => s + a.imports, 0),
366
+ totalFuncs: analyses.reduce((s, a) => s + a.funcs, 0),
367
+ totalClasses: analyses.reduce((s, a) => s + a.classes, 0)
368
+ };
369
+ } catch {}
370
+ // 1.9.242: envInfo 통합 (session close JSON 10번째 통합 필드) — UR-0014 2단계
371
+ try {
372
+ const runtimeEnv = _collectRuntimeEnv();
373
+ const encScan = _scanShellScriptsEncoding(root);
374
+ jsonResult.envInfo = {
375
+ os: runtimeEnv.os.platform,
376
+ isKoreanWindows: runtimeEnv.locale.isKoreanWindows || false,
377
+ codepage: runtimeEnv.locale.codepage || null,
378
+ nodeVersion: runtimeEnv.node.version,
379
+ shellScriptsScanned: encScan.scanned,
380
+ encodingRiskCount: encScan.atRisk.length,
381
+ encodingRiskFiles: encScan.atRisk.slice(0, 5).map(r => r.file),
382
+ // 1.9.249 (UR-0018): 터미널 출력 인코딩 안전 여부 + 자동 회복 결과
383
+ terminalEncodingOk: runtimeEnv.locale.codepage === 65001 || !runtimeEnv.locale.isKoreanWindows,
384
+ autoChcpApplied: process.env._LEERNESS_AUTOCHCP_APPLIED || null,
385
+ // 1.9.250 (UR-0018 2단계): POSIX (Linux/macOS/WSL) terminal encoding 점검
386
+ posixEncodingOk: runtimeEnv.locale.posixEncodingOk,
387
+ isWSL: runtimeEnv.locale.isWSL || false
388
+ };
389
+ } catch {}
390
+ // 1.9.245: apiSkills 통합 (session close JSON 11번째 통합 필드) — UR-0015
391
+ try {
392
+ const allSkills = _listAPISkills(root);
393
+ let currentTaskText = '';
394
+ try {
395
+ const rows = readProgressRows(root);
396
+ const ip = rows.find(r => r.status === 'in-progress');
397
+ if (ip) currentTaskText = (ip.title || '') + ' ' + (ip.notes || '');
398
+ } catch {}
399
+ const matched = currentTaskText ? _matchAPISkills(root, currentTaskText) : [];
400
+ jsonResult.apiSkills = {
401
+ total: allSkills.length,
402
+ matched: matched.length,
403
+ matchedIds: matched.slice(0, 5).map(s => s.id),
404
+ ids: allSkills.slice(0, 10).map(s => s.id)
405
+ };
406
+ } catch {}
407
+ // 1.9.264: shellGuard 통합 (session close JSON 12번째 통합 필드) — UR-0020 셸 실패 메모리 + 환경 변동
408
+ try {
409
+ const sf = _loadShellFailures(root);
410
+ const drift = _shellEnvDrift(root);
411
+ jsonResult.shellGuard = {
412
+ failureCount: sf.failures.length,
413
+ recent: sf.failures.slice(-3).map(f => ({ cmd: (f.cmd || '').slice(0, 50), exitCode: f.exitCode, shell: f.shell, rules: f.issues || [] })),
414
+ envDriftChanges: drift && drift.changes ? drift.changes.length : 0,
415
+ envDrift: drift ? drift.changes : null
416
+ };
417
+ } catch {}
418
+ } catch {}
419
+ try {
420
+ // 1.9.209: pre-wake-audit 자동 실행 + 저장 (sleep 전 자동 점검)
421
+ if (!opts.noPreWake && !has('--no-pre-wake')) {
422
+ const audit = _runPreWakeAudit(root);
423
+ _saveAndAppendPreWakeReport(root, audit);
424
+ jsonResult.preWakeAudit = {
425
+ auditedAt: audit.auditedAt,
426
+ critical: audit.summary.criticalCount,
427
+ warning: audit.summary.warningCount,
428
+ info: audit.summary.infoCount,
429
+ needsAttention: audit.summary.needsAttention
430
+ };
431
+ }
432
+ } catch {}
433
+ try {
434
+ // 1.9.212: 멱등성 검사 자동 실행 (rule/task/user-requests/wakeups 4영역)
435
+ const idemp = _runIdempotencyAudit(root);
436
+ jsonResult.idempotencyAudit = {
437
+ violations: idemp.summary.totalViolations,
438
+ high: idemp.summary.highSeverity,
439
+ medium: idemp.summary.mediumSeverity,
440
+ low: idemp.summary.lowSeverity,
441
+ verified: idemp.summary.verifiedAreas,
442
+ overall: idemp.summary.overall
443
+ };
444
+ } catch {}
445
+ try {
446
+ // 1.9.221: abnormalShutdown 자동 감지 (1.9.220 통합) — session close 시 다음 재개 가이드 회수
447
+ const ad = _detectAbnormalShutdown(root);
448
+ jsonResult.abnormalShutdown = {
449
+ detected: ad.abnormalShutdown,
450
+ severity: ad.severity,
451
+ signalCount: ad.signals.length,
452
+ signals: ad.signals.map(s => ({ kind: s.kind, severity: s.severity, detail: s.detail })),
453
+ resumeGuide: ad.resumeGuide
454
+ };
455
+ } catch {}
456
+
457
+ process.stdout.write(JSON.stringify(jsonResult, null, 2) + '\n');
458
+ } else {
459
+ // 1.9.217: human 출력 모드에서도 통합 보고 노출 (마감 직전)
460
+ try {
461
+ const isTty = process.stdout.isTTY;
462
+ const grn = s => isTty ? `\x1b[32m${s}\x1b[0m` : s;
463
+ const yel = s => isTty ? `\x1b[33m${s}\x1b[0m` : s;
464
+ const red = s => isTty ? `\x1b[31m${s}\x1b[0m` : s;
465
+ const dim = s => isTty ? `\x1b[2m${s}\x1b[0m` : s;
466
+
467
+ log('');
468
+ log(`## 🔚 session close 자동 통합 보고 (1.9.217)`);
469
+ // 1.9.207 + 1.9.223 (delivered 패턴 자동 권장) + 1.9.224 (--auto-apply-delivered 옵션)
470
+ try {
471
+ const reqAudit = _auditUserRequests(root);
472
+ const missCnt = reqAudit.missing ? reqAudit.missing.length : 0;
473
+ let delivered = { candidates: [] };
474
+ try { delivered = _detectDeliveredRequests(root); } catch {}
475
+ if (delivered.candidates && delivered.candidates.length > 0) {
476
+ if (has('--auto-apply-delivered')) {
477
+ // 1.9.224: 자동 정리 (마감 시 호출 — 안전: 패턴 매칭 + 버전 가드)
478
+ let ok = 0;
479
+ for (const c of delivered.candidates) {
480
+ const u = _updateUserRequest(root, c.id, { status: 'completed', autoCompletedAt: new Date().toISOString(), autoCompleteReason: 'session-close-auto-apply-1.9.224' });
481
+ if (u) ok++;
482
+ }
483
+ log(grn(` ✓ delivered 패턴 ${ok}건 자동 완료 (--auto-apply-delivered 1.9.224)`));
484
+ } else {
485
+ log(yel(` 📥 delivered 패턴 ${delivered.candidates.length}건 (1.9.223) 자동 완료 가능`));
486
+ log(dim(` → leerness requests auto-complete --apply (수동) 또는 session close --auto-apply-delivered (1.9.224)`));
487
+ }
488
+ } else if (missCnt > 0) {
489
+ log(red(` 미답 사용자 요청 ${missCnt}건 (task-log/plan/decisions 매칭 안 됨)`));
490
+ } else if (reqAudit.open > 0) {
491
+ log(grn(` ✓ 사용자 요청 ${reqAudit.open}건 모두 tracked`));
492
+ } else {
493
+ log(dim(` ℹ 사용자 요청 없음 (UR 백로그 비어있음)`));
494
+ }
495
+ } catch {}
496
+ // 1.9.209
497
+ try {
498
+ if (!opts.noPreWake && !has('--no-pre-wake')) {
499
+ const audit = _runPreWakeAudit(root);
500
+ _saveAndAppendPreWakeReport(root, audit);
501
+ const sum = audit.summary;
502
+ if (sum.criticalCount > 0) {
503
+ log(red(` 🚨 pre-wake-audit: critical ${sum.criticalCount} (다음 깨어남 시 점검 필요)`));
504
+ } else if (sum.warningCount > 0) {
505
+ log(yel(` ⚠ pre-wake-audit: warning ${sum.warningCount}`));
506
+ } else {
507
+ log(grn(` ✓ pre-wake-audit: clean (sleep 안전)`));
508
+ }
509
+ }
510
+ } catch {}
511
+ // 1.9.212
512
+ try {
513
+ const idemp = _runIdempotencyAudit(root);
514
+ const v = idemp.summary.totalViolations;
515
+ if (v > 0) {
516
+ log(red(` ⚠ 멱등성 위반 ${v}건 (high: ${idemp.summary.highSeverity})`));
517
+ log(dim(` → leerness idempotency audit 으로 상세 확인`));
518
+ } else {
519
+ log(grn(` ✓ 멱등성 검사 통과 — verified ${idemp.summary.verifiedAreas} 영역`));
520
+ }
521
+ } catch {}
522
+ // 1.9.264: 실패 메모리 + 환경 변동 요약 (UR-0020) — 마감 시 이번 세션 셸 실패를 회고에 노출
523
+ try {
524
+ const sf = _loadShellFailures(root);
525
+ const drift = _shellEnvDrift(root);
526
+ const driftN = drift && drift.changes ? drift.changes.length : 0;
527
+ if (sf.failures.length > 0 || driftN > 0) {
528
+ if (driftN > 0) log(yel(` ⚠ 환경 버전 변동 ${driftN}건 — 다음 세션 셸 실패 기록 재검토 권장`));
529
+ if (sf.failures.length > 0) {
530
+ log(yel(` 🐚 셸 실패 누적 ${sf.failures.length}건 다음 handoff 가 자동 노출`));
531
+ log(dim(` → 명령 실행 전 점검: leerness shell-guard "<command>"`));
532
+ }
533
+ } else {
534
+ log(grn(` ✓ 실패 기록 없음 (터미널 호환성 양호)`));
535
+ }
536
+ } catch {}
537
+ // 1.9.237: session close --auto-cleanup-branches 50+ release/* branches 자동 정리
538
+ // 1.9.224 패턴 (--auto-apply-delivered) 확장 — 마감 시 운영 누적 폐기물 자동 정리
539
+ // 안전: keep 10, merged 만, 현재 branch 보호
540
+ try {
541
+ const branchR = cp.spawnSync('git', ['branch', '--merged', 'main', '--list', 'release/*'], { cwd: root, encoding: 'utf8' });
542
+ if (branchR.status === 0) {
543
+ const merged = (branchR.stdout || '').split('\n')
544
+ .map(l => l.replace(/^\*?\s+/, '').trim())
545
+ .filter(l => l && /^release\/\d+\.\d+\.\d+$/.test(l));
546
+ if (merged.length > 50) {
547
+ if (has('--auto-cleanup-branches')) {
548
+ merged.sort((a, b) => {
549
+ const va = a.replace('release/', '').split('.').map(n => parseInt(n, 10) || 0);
550
+ const vb = b.replace('release/', '').split('.').map(n => parseInt(n, 10) || 0);
551
+ for (let i = 0; i < 3; i++) if (va[i] !== vb[i]) return vb[i] - va[i];
552
+ return 0;
553
+ });
554
+ const curR = cp.spawnSync('git', ['rev-parse', '--abbrev-ref', 'HEAD'], { cwd: root, encoding: 'utf8' });
555
+ const cur = (curR.stdout || '').trim();
556
+ const toDelete = merged.slice(10).filter(b => b !== cur);
557
+ let okCnt = 0;
558
+ for (const b of toDelete) {
559
+ const r = cp.spawnSync('git', ['branch', '-d', b], { cwd: root, encoding: 'utf8' });
560
+ if (r.status === 0) okCnt++;
561
+ }
562
+ log(grn(` release 정리 ${okCnt}/${toDelete.length}건 (--auto-cleanup-branches 1.9.237, keep 10)`));
563
+ } else {
564
+ log(yel(` 🗑 release/* merged ${merged.length}개 (50+) — cleanup 가능 (1.9.235)`));
565
+ log(dim(` → leerness release cleanup --apply --keep 10 (수동)`));
566
+ log(dim(` → 또는 session close --auto-cleanup-branches (1.9.237 자동)`));
567
+ }
568
+ }
569
+ }
570
+ } catch {}
571
+ // 1.9.243: session close --auto-fix-encoding — 셸 스크립트 인코딩 위험 자동 회복 (UR-0014 3단계)
572
+ // 1.9.224 (--auto-apply-delivered) / 1.9.237 (--auto-cleanup-branches) 패턴 확장
573
+ // 마감 한국어/일본어/중국어 PowerShell 인코딩 위험 자동 BOM 추가
574
+ try {
575
+ const encScan = _scanShellScriptsEncoding(root);
576
+ if (encScan.atRisk && encScan.atRisk.length > 0) {
577
+ if (has('--auto-fix-encoding')) {
578
+ let ok = 0;
579
+ for (const r of encScan.atRisk) {
580
+ try {
581
+ const fullPath = path.join(root, r.file);
582
+ const orig = fs.readFileSync(fullPath);
583
+ const bom = Buffer.from([0xEF, 0xBB, 0xBF]);
584
+ const fixed = Buffer.concat([bom, orig]);
585
+ fs.writeFileSync(fullPath, fixed);
586
+ ok++;
587
+ } catch {}
588
+ }
589
+ log(grn(` ✓ 인코딩 위험 ${ok}/${encScan.atRisk.length}건 UTF-8 BOM 자동 추가 (--auto-fix-encoding 1.9.243)`));
590
+ } else {
591
+ log(yel(` ⚠ 스크립트 인코딩 위험 ${encScan.atRisk.length}건 (1.9.241) — 자동 회복 가능`));
592
+ log(dim(` → leerness env encoding --apply (수동) 또는 session close --auto-fix-encoding (1.9.243 자동)`));
593
+ }
594
+ }
595
+ } catch {}
596
+ // 1.9.232: 마감 시 pulse 한 줄 자동 노출 — 다음 라운드 진입 시 즉시 상태 인지
597
+ try {
598
+ const rh = _computeRoundHistory(root);
599
+ const ms = _computeMilestones(root);
600
+ const rows = readProgressRows(root);
601
+ const tIn = rows.filter(r => r.status === 'in-progress').length;
602
+ const dCnt = _loadDecisions(root).length; // 1.9.339 (UR-0053): canonical 단일 진실소스
603
+ const rActive = readRules(root).filter(r => r.status === 'active').length;
604
+ const planText = exists(planPath(root)) ? read(planPath(root)) : '';
605
+ const pCnt = (planText.match(/^### M-\d{4}\./gm) || []).length;
606
+ const lCnt = _loadLessons(root).length;
607
+ const mem = `T${tIn}/D${dCnt}/R${rActive}/P${pCnt}/L${lCnt}`;
608
+ let pulseLine = `📍 v${VERSION} · 🔄 R${rh.roundCount} · 🧠 ${mem}`;
609
+ if (ms.next) {
610
+ const eta = ms.next.etaDays != null ? ` (${ms.next.etaDays}d)` : '';
611
+ pulseLine += ` · 🎯 R${ms.next.milestone}${eta}`;
612
+ }
613
+ log('');
614
+ log(` ${pulseLine} ${dim('— leerness pulse (1.9.232)')}`);
615
+ } catch {}
616
+ } catch {}
617
+ }
618
+ }
619
+
620
+ module.exports = { sessionClose };