leerness 1.27.0 → 1.29.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/CHANGELOG.md +88 -0
- package/README.md +4 -4
- package/bin/leerness.js +38 -6
- package/lib/audit.js +5 -0
- package/lib/diagnostics.js +44 -41
- package/lib/drift.js +343 -341
- package/package.json +1 -1
- package/scripts/e2e.js +45 -2
package/lib/drift.js
CHANGED
|
@@ -1,341 +1,343 @@
|
|
|
1
|
-
// lib/drift.js — drift check 핸들러 (UR-0025/UR-0125 큰 핸들러 모듈화 7번째, 1.9.422)
|
|
2
|
-
// bin/leerness.js 에서 driftCheckCmd(322줄) 분리. DI: harness 고유 의존(VERSION, has, arg, harnessPath, readProgressRows, planPath, handoffPath, currentStatePath, _usageStatsPath, _readUsageStats, _updateUserRequest, _scanShellScriptsEncoding, _readFeatureGraph, _detectDeliveredRequests, _autoFixIdempotency) 주입.
|
|
3
|
-
// io 프리미티브는 ./io, cp/path 빌트인. 내부 재귀(auto-fix 후 재검사)는 deps 전달. 동작/출력 무변경.
|
|
4
|
-
'use strict';
|
|
5
|
-
const cp = require('child_process');
|
|
6
|
-
const path = require('path');
|
|
7
|
-
const fs = require('fs');
|
|
8
|
-
const { log, ok, warn, fail, failJson, today, now, absRoot, exists, read, readBuf, mkdirp, writeUtf8, append, rel } = require('./io');
|
|
9
|
-
|
|
10
|
-
function driftCheckCmd(root, opts = {}, deps = {}) {
|
|
11
|
-
const { VERSION, has, arg, harnessPath, readProgressRows, planPath, handoffPath, currentStatePath, taskLogPath, envDiff, _usageStatsPath, _readUsageStats, _updateUserRequest, _scanShellScriptsEncoding, _readFeatureGraph, _detectDeliveredRequests, _autoFixIdempotency } = deps;
|
|
12
|
-
root = absRoot(root || process.cwd());
|
|
13
|
-
// 1.
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
const
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
const
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
const
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
const
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
const
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
const
|
|
95
|
-
const
|
|
96
|
-
const
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
const
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
const
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
else if (totalScore >=
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
stats
|
|
155
|
-
stats.drift
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
// 1.9.
|
|
163
|
-
// 1.9.
|
|
164
|
-
//
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
//
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
afLog(
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
const
|
|
204
|
-
const
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
//
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
//
|
|
252
|
-
//
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
merged.
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
const
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
}
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
stats
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
}
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
}
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
log(
|
|
317
|
-
log(
|
|
318
|
-
log('');
|
|
319
|
-
log(
|
|
320
|
-
log(
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
log(
|
|
334
|
-
log(
|
|
335
|
-
log(` -
|
|
336
|
-
log(` -
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
}
|
|
340
|
-
|
|
341
|
-
|
|
1
|
+
// lib/drift.js — drift check 핸들러 (UR-0025/UR-0125 큰 핸들러 모듈화 7번째, 1.9.422)
|
|
2
|
+
// bin/leerness.js 에서 driftCheckCmd(322줄) 분리. DI: harness 고유 의존(VERSION, has, arg, harnessPath, readProgressRows, planPath, handoffPath, currentStatePath, _usageStatsPath, _readUsageStats, _updateUserRequest, _scanShellScriptsEncoding, _readFeatureGraph, _detectDeliveredRequests, _autoFixIdempotency) 주입.
|
|
3
|
+
// io 프리미티브는 ./io, cp/path 빌트인. 내부 재귀(auto-fix 후 재검사)는 deps 전달. 동작/출력 무변경.
|
|
4
|
+
'use strict';
|
|
5
|
+
const cp = require('child_process');
|
|
6
|
+
const path = require('path');
|
|
7
|
+
const fs = require('fs');
|
|
8
|
+
const { log, ok, warn, fail, failJson, today, now, absRoot, exists, read, readBuf, mkdirp, writeUtf8, append, rel } = require('./io');
|
|
9
|
+
|
|
10
|
+
function driftCheckCmd(root, opts = {}, deps = {}) {
|
|
11
|
+
const { VERSION, has, arg, uiLang, harnessPath, readProgressRows, planPath, handoffPath, currentStatePath, taskLogPath, envDiff, _usageStatsPath, _readUsageStats, _updateUserRequest, _scanShellScriptsEncoding, _readFeatureGraph, _detectDeliveredRequests, _autoFixIdempotency } = deps;
|
|
12
|
+
root = absRoot(root || process.cwd());
|
|
13
|
+
const t = (ko, en) => (uiLang === 'en' ? en : ko); // 1.27.2 (UR-0010 Phase 10): drift 출력 영어 opt-in (--auto-fix 진행로그는 Phase 10b)
|
|
14
|
+
// 1.9.434 (11th 외부평가 Opus P2, UR-0136): 미존재 경로는 healthy 위조 금지 — failJson + exit 1.
|
|
15
|
+
if (!exists(root)) { failJson(has('--json'), 'path_not_found', t(`경로 없음: ${root}`, `path not found: ${root}`)); return; }
|
|
16
|
+
const now = Date.now();
|
|
17
|
+
const _ageDays = (p) => {
|
|
18
|
+
if (!exists(p)) return null;
|
|
19
|
+
return (now - fs.statSync(p).mtimeMs) / 86400000;
|
|
20
|
+
};
|
|
21
|
+
// 각 메타파일의 마지막 갱신
|
|
22
|
+
const signals = [];
|
|
23
|
+
// 1. session-handoff.md - "Last generated" 라인 우선, 없으면 mtime
|
|
24
|
+
const shPath = handoffPath(root);
|
|
25
|
+
if (exists(shPath)) {
|
|
26
|
+
const txt = read(shPath);
|
|
27
|
+
// 1.9.316 (drift 마커 버그): 최신(마지막) 'Last generated' 사용 — 구 블록 중복 시 첫(구) 매치를 읽던 오발화 방어.
|
|
28
|
+
const allGen = [...txt.matchAll(/Last generated:\s*([\d\-T:.Z]+)/g)];
|
|
29
|
+
const m = allGen.length ? allGen[allGen.length - 1] : null;
|
|
30
|
+
let ageDays;
|
|
31
|
+
if (m) {
|
|
32
|
+
ageDays = (now - new Date(m[1]).getTime()) / 86400000;
|
|
33
|
+
} else {
|
|
34
|
+
ageDays = _ageDays(shPath);
|
|
35
|
+
}
|
|
36
|
+
signals.push({ file: 'session-handoff.md', ageDays, threshold: 1, weight: 30, label: t('session close 누락', 'session close missing') });
|
|
37
|
+
}
|
|
38
|
+
// 2. current-state.md - "Updated: YYYY-MM-DD" 라인
|
|
39
|
+
const csPath = currentStatePath(root);
|
|
40
|
+
if (exists(csPath)) {
|
|
41
|
+
const m = read(csPath).match(/Updated:\s*(\d{4}-\d{2}-\d{2})/);
|
|
42
|
+
const ageDays = m ? (now - new Date(m[1]).getTime()) / 86400000 : _ageDays(csPath);
|
|
43
|
+
signals.push({ file: 'current-state.md', ageDays, threshold: 2, weight: 20, label: t('current-state 갱신 없음', 'current-state not updated') });
|
|
44
|
+
}
|
|
45
|
+
// 3. progress-tracker.md 마지막 row의 updated 컬럼
|
|
46
|
+
const rows = readProgressRows(root);
|
|
47
|
+
if (rows.length) {
|
|
48
|
+
const dates = rows.map(r => (r.updated || '').match(/\d{4}-\d{2}-\d{2}/)).filter(Boolean).map(m => m[0]);
|
|
49
|
+
if (dates.length) {
|
|
50
|
+
dates.sort();
|
|
51
|
+
const latest = dates[dates.length - 1];
|
|
52
|
+
const ageDays = (now - new Date(latest).getTime()) / 86400000;
|
|
53
|
+
signals.push({ file: 'progress-tracker.md', ageDays, threshold: 1, weight: 30, label: t('task update 없음', 'no task update') });
|
|
54
|
+
}
|
|
55
|
+
} else {
|
|
56
|
+
signals.push({ file: 'progress-tracker.md', ageDays: 999, threshold: 1, weight: 25, label: t('progress-tracker 비어있음', 'progress-tracker empty') });
|
|
57
|
+
}
|
|
58
|
+
// 4. task-log.md 마지막 entry "## YYYY-MM-DD"
|
|
59
|
+
const tlPath = taskLogPath(root);
|
|
60
|
+
if (exists(tlPath)) {
|
|
61
|
+
const dates = Array.from(read(tlPath).matchAll(/^## (\d{4}-\d{2}-\d{2})/gm)).map(m => m[1]);
|
|
62
|
+
if (dates.length) {
|
|
63
|
+
dates.sort();
|
|
64
|
+
const latest = dates[dates.length - 1];
|
|
65
|
+
const ageDays = (now - new Date(latest).getTime()) / 86400000;
|
|
66
|
+
signals.push({ file: 'task-log.md', ageDays, threshold: 2, weight: 20, label: t('task-log 갱신 없음', 'task-log not updated') });
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
// 점수 계산
|
|
70
|
+
let totalScore = 0;
|
|
71
|
+
const fired = [];
|
|
72
|
+
for (const s of signals) {
|
|
73
|
+
if (s.ageDays > s.threshold) {
|
|
74
|
+
totalScore += s.weight;
|
|
75
|
+
fired.push(s);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
// 1.9.78: 보안 신호 (env / .gitignore 누락) — 5번째 신호
|
|
79
|
+
try {
|
|
80
|
+
const envPath = path.join(root, '.env');
|
|
81
|
+
if (exists(envPath)) {
|
|
82
|
+
let secScore = 0;
|
|
83
|
+
const secIssues = [];
|
|
84
|
+
// (a) .env vs .env.example 동기화
|
|
85
|
+
try {
|
|
86
|
+
const d = envDiff(root);
|
|
87
|
+
if (d.inEnvOnly.length) {
|
|
88
|
+
secIssues.push(t(`.env→.env.example 누락 ${d.inEnvOnly.length}건`, `.env→.env.example missing ${d.inEnvOnly.length}`));
|
|
89
|
+
secScore += 15;
|
|
90
|
+
}
|
|
91
|
+
} catch {}
|
|
92
|
+
// (b) .gitignore 시크릿 패턴
|
|
93
|
+
try {
|
|
94
|
+
const giText = exists(path.join(root, '.gitignore')) ? read(path.join(root, '.gitignore')) : '';
|
|
95
|
+
const giLines = giText.split('\n').map(l => l.trim());
|
|
96
|
+
const SECRET_PATTERNS = ['.env', '.env.local', '.env.production', '.env.*.local', '*.pem', 'credentials.json'];
|
|
97
|
+
const missing = SECRET_PATTERNS.filter(p => !giLines.some(l => l === p || l === '/' + p));
|
|
98
|
+
if (missing.length) {
|
|
99
|
+
secIssues.push(t(`.gitignore 시크릿 누락 ${missing.length}건`, `.gitignore missing secret patterns ${missing.length}`));
|
|
100
|
+
// 누락이 .env 자체면 최우선 위험 — 15점 가중
|
|
101
|
+
if (missing.includes('.env')) secScore += 30;
|
|
102
|
+
else secScore += Math.min(20, missing.length * 5);
|
|
103
|
+
}
|
|
104
|
+
} catch {}
|
|
105
|
+
if (secScore > 0) {
|
|
106
|
+
totalScore += secScore;
|
|
107
|
+
fired.push({ file: '.env / .gitignore', ageDays: null, threshold: 0, weight: secScore, label: t(`보안 위험 (1.9.78): ${secIssues.join(' · ')}`, `security risk: ${secIssues.join(' · ')}`) });
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
} catch {}
|
|
111
|
+
// 1.9.143: Feature Graph 미사용 신호 — 노드는 있는데 edges 비율 낮으면 인과관계 정리 미진
|
|
112
|
+
try {
|
|
113
|
+
const { nodes: fGraphNodes } = _readFeatureGraph(root);
|
|
114
|
+
if (fGraphNodes.length >= 3) {
|
|
115
|
+
const edgeCount = fGraphNodes.reduce((s, n) => s + (n.dependsOn?.length || 0) + (n.affects?.length || 0) + (n.coChangesWith?.length || 0), 0);
|
|
116
|
+
const linkedSet = new Set();
|
|
117
|
+
for (const n of fGraphNodes) {
|
|
118
|
+
for (const x of [...(n.dependsOn||[]), ...(n.affects||[]), ...(n.coChangesWith||[])]) { linkedSet.add(n.id); linkedSet.add(x); }
|
|
119
|
+
}
|
|
120
|
+
const isolatedCount = Math.max(0, fGraphNodes.length - linkedSet.size);
|
|
121
|
+
const isolatedRatio = isolatedCount / fGraphNodes.length;
|
|
122
|
+
if (edgeCount === 0 || isolatedRatio >= 0.5) {
|
|
123
|
+
const fgScore = edgeCount === 0 ? 25 : 15;
|
|
124
|
+
totalScore += fgScore;
|
|
125
|
+
fired.push({ file: '.harness/feature-graph.md', ageDays: null, threshold: 0, weight: fgScore, label: t(`Feature Graph 미정리 (1.9.143): ${fGraphNodes.length} 노드, edges=${edgeCount}, isolated=${isolatedCount}`, `Feature Graph unlinked: ${fGraphNodes.length} nodes, edges=${edgeCount}, isolated=${isolatedCount}`) });
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
} catch {}
|
|
129
|
+
// 신규 _apps/* 에서 task 0건도 신호로
|
|
130
|
+
const appsDir = path.join(root, '_apps');
|
|
131
|
+
let appsZeroTask = [];
|
|
132
|
+
if (exists(appsDir)) {
|
|
133
|
+
for (const d of fs.readdirSync(appsDir)) {
|
|
134
|
+
const sub = path.join(appsDir, d);
|
|
135
|
+
if (!exists(path.join(sub, '.harness'))) continue;
|
|
136
|
+
const subRows = readProgressRows(sub);
|
|
137
|
+
if (!subRows.length) appsZeroTask.push(d);
|
|
138
|
+
}
|
|
139
|
+
if (appsZeroTask.length) {
|
|
140
|
+
const w = Math.min(50, appsZeroTask.length * 10);
|
|
141
|
+
totalScore += w;
|
|
142
|
+
fired.push({ file: `_apps/* (${appsZeroTask.length}개)`, ageDays: null, threshold: 0, weight: w, label: t(`task 0건 sub-app: ${appsZeroTask.slice(0, 3).join(', ')}${appsZeroTask.length > 3 ? '...' : ''}`, `sub-app with 0 tasks: ${appsZeroTask.slice(0, 3).join(', ')}${appsZeroTask.length > 3 ? '...' : ''}`) });
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
// 레벨 판정
|
|
146
|
+
let level = '🟢 healthy';
|
|
147
|
+
if (totalScore >= 100) level = '🔴 critical';
|
|
148
|
+
else if (totalScore >= 50) level = '🟡 warning';
|
|
149
|
+
else if (totalScore >= 20) level = '🟠 attention';
|
|
150
|
+
|
|
151
|
+
// 1.9.38 (D): drift critical 등급은 누적 카운트 (학습 신호)
|
|
152
|
+
try {
|
|
153
|
+
if (level === '🔴 critical') {
|
|
154
|
+
const stats = _readUsageStats(root);
|
|
155
|
+
stats.drift = stats.drift || {};
|
|
156
|
+
stats.drift.criticalSeen = (stats.drift.criticalSeen || 0) + 1;
|
|
157
|
+
const p = _usageStatsPath(root);
|
|
158
|
+
mkdirp(path.dirname(p));
|
|
159
|
+
writeUtf8(p, JSON.stringify(stats, null, 2) + '\n');
|
|
160
|
+
}
|
|
161
|
+
} catch {}
|
|
162
|
+
// 1.9.39: --auto-fix — critical 시 session close 자동 실행
|
|
163
|
+
// 1.9.82: --auto-fix가 보안 신호도 자동 회복 (audit --fix 호출)
|
|
164
|
+
// 1.9.432 (10th 외부평가 Opus latent, UR-0131 잔여): depth 가드 — 재귀 호출(_noAutoFix)은 auto-fix 재진입 금지.
|
|
165
|
+
// 기존엔 autoFix=has('--auto-fix')가 전역 argv 재독→재귀도 auto-fix 분기 재진입, 종료는 'audit이 보안신호를 지운다'는 취약 불변식에 의존(미래 신호 타입이 비가역이면 무한재귀). 명시 1회 보장.
|
|
166
|
+
const autoFix = has('--auto-fix') && !opts._noAutoFix;
|
|
167
|
+
// 1.9.439 (10th 외부평가 Codex P1, UR-0135): --json 모드면 auto-fix 진행로그 억제(stdout 순수 JSON 보장).
|
|
168
|
+
// 재귀(_noAutoFix)는 auto-fix 블록을 건너뛰고 마지막 JSON(아래 has('--json') 블록)만 출력 → afLog 로 첫 패스 진행로그만 무음화.
|
|
169
|
+
const afLog = has('--json') ? () => {} : log;
|
|
170
|
+
// 1.9.82: 보안 신호가 fired에 있으면 우선 audit --fix 호출
|
|
171
|
+
// 1.28.1 (Phase 10b): 보안 신호 판정을 *언어-안정* 한 file 필드로 — 1.27.2 에서 label 을 언어화하면서 한국어 라벨 regex 매칭이 --language en 에서 깨지던 버그 수정(보안 auto-fix 미발동 방지).
|
|
172
|
+
const hasSecurityFired = fired.some(f => f.file === '.env / .gitignore');
|
|
173
|
+
if (autoFix && hasSecurityFired) {
|
|
174
|
+
afLog('');
|
|
175
|
+
afLog(t(`🔒 --auto-fix 활성 (1.9.82) — 보안 신호 회복: audit --fix 자동 실행 중...`, `🔒 --auto-fix on — recovering security signal: running audit --fix...`));
|
|
176
|
+
try {
|
|
177
|
+
const r = cp.spawnSync(process.execPath, [harnessPath, 'audit', root, '--fix'],
|
|
178
|
+
{ encoding: 'utf8', timeout: 30000, env: { ...process.env, LEERNESS_INTERNAL: '1', LEERNESS_NO_PROMPT: '1', LEERNESS_NO_DRIFT_CHECK: '1' } });
|
|
179
|
+
if (r.status === 0) {
|
|
180
|
+
afLog(t(`✓ audit --fix 완료 — .gitignore + .env.example 동기화`, `✓ audit --fix done — .gitignore + .env.example synced`));
|
|
181
|
+
// 재검사 (보안 신호 회복 확인)
|
|
182
|
+
afLog('');
|
|
183
|
+
afLog(t(`재검사 중...`, `re-checking...`));
|
|
184
|
+
return driftCheckCmd(root, { ...opts, _noAutoFix: true }, deps); // 재귀 1회 (auto-fix 없이, 1.9.432 depth 가드)
|
|
185
|
+
} else {
|
|
186
|
+
afLog(t(`⚠ audit --fix 실패 (exit ${r.status}) — 수동 \`leerness audit --fix\` 권장`, `⚠ audit --fix failed (exit ${r.status}) — run \`leerness audit --fix\` manually`));
|
|
187
|
+
}
|
|
188
|
+
} catch (e) {
|
|
189
|
+
afLog(t(`⚠ auto-fix 보안 회복 오류: ${e.message}`, `⚠ auto-fix security recovery error: ${e.message}`));
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
// 1.9.242: drift check --auto-fix 에 env encoding BOM 자동 추가 통합 (사용자 명시 UR-0014 2단계)
|
|
193
|
+
// 1.9.82 패턴 확장 — drift 회복 시 셸 스크립트 인코딩 위험도 자동 해결
|
|
194
|
+
if (autoFix) {
|
|
195
|
+
try {
|
|
196
|
+
const encScan = _scanShellScriptsEncoding(root);
|
|
197
|
+
if (encScan.atRisk && encScan.atRisk.length > 0) {
|
|
198
|
+
afLog('');
|
|
199
|
+
afLog(t(`🌐 --auto-fix 활성 (1.9.242) — 셸 스크립트 인코딩 위험 ${encScan.atRisk.length}건 BOM 자동 추가 중...`, `🌐 --auto-fix on — adding UTF-8 BOM to ${encScan.atRisk.length} at-risk shell script(s)...`));
|
|
200
|
+
let ok = 0;
|
|
201
|
+
for (const r of encScan.atRisk) {
|
|
202
|
+
try {
|
|
203
|
+
const fullPath = path.join(root, r.file);
|
|
204
|
+
const orig = fs.readFileSync(fullPath);
|
|
205
|
+
const bom = Buffer.from([0xEF, 0xBB, 0xBF]);
|
|
206
|
+
const fixed = Buffer.concat([bom, orig]);
|
|
207
|
+
fs.writeFileSync(fullPath, fixed);
|
|
208
|
+
ok++;
|
|
209
|
+
} catch {}
|
|
210
|
+
}
|
|
211
|
+
afLog(t(`✓ UTF-8 BOM 추가 ${ok}/${encScan.atRisk.length}건 (1.9.242 UR-0014)`, `✓ added UTF-8 BOM ${ok}/${encScan.atRisk.length}`));
|
|
212
|
+
}
|
|
213
|
+
} catch (e) {
|
|
214
|
+
afLog(t(`⚠ env encoding auto-fix 오류 (1.9.242): ${e.message}`, `⚠ env encoding auto-fix error: ${e.message}`));
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
// 1.9.225: drift check --auto-fix 에 delivered 패턴 자동 적용 통합 (1.9.223/224 시스템 회수)
|
|
218
|
+
// 사용자 요청에 "구현 완료" 패턴이 누적되면 가짜 미답 신호가 drift score 를 가중시킬 수 있음 → 자동 정리.
|
|
219
|
+
// 1.9.82 audit --fix 패턴과 동일: --auto-fix 시 즉시 적용, 적용 후 재검사.
|
|
220
|
+
if (autoFix) {
|
|
221
|
+
try {
|
|
222
|
+
const delivered = _detectDeliveredRequests(root);
|
|
223
|
+
if (delivered.candidates && delivered.candidates.length > 0) {
|
|
224
|
+
afLog('');
|
|
225
|
+
afLog(t(`📥 --auto-fix 활성 (1.9.225) — delivered 패턴 ${delivered.candidates.length}건 자동 완료 중...`, `📥 --auto-fix on — auto-completing ${delivered.candidates.length} delivered pattern(s)...`));
|
|
226
|
+
let ok = 0;
|
|
227
|
+
for (const c of delivered.candidates) {
|
|
228
|
+
const u = _updateUserRequest(root, c.id, { status: 'completed', autoCompletedAt: new Date().toISOString(), autoCompleteReason: 'drift-auto-fix-1.9.225' });
|
|
229
|
+
if (u) ok++;
|
|
230
|
+
}
|
|
231
|
+
afLog(t(`✓ delivered 자동 완료 ${ok}/${delivered.candidates.length}건`, `✓ delivered auto-completed ${ok}/${delivered.candidates.length}`));
|
|
232
|
+
}
|
|
233
|
+
} catch (e) {
|
|
234
|
+
afLog(t(`⚠ delivered auto-apply 오류 (1.9.225): ${e.message}`, `⚠ delivered auto-apply error: ${e.message}`));
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
// 1.9.293: drift check --auto-fix 에 idempotency task/user-request 중복 자동 정리 통합
|
|
238
|
+
// 누적 중복 task/요청이 idempotency 위반(medium)을 가중 → drift/handoff 노이즈. 안전: 완전중복 행 제거 + 동일텍스트 dropped 보존(id 유지).
|
|
239
|
+
if (autoFix) {
|
|
240
|
+
try {
|
|
241
|
+
const idemFixes = _autoFixIdempotency(root);
|
|
242
|
+
const totalFixed = idemFixes.reduce((n, f) => n + (f.removedExact || 0) + (f.droppedSameText || 0) + (f.count || 0), 0);
|
|
243
|
+
if (totalFixed > 0) {
|
|
244
|
+
afLog('');
|
|
245
|
+
afLog(t(`🔁 --auto-fix 활성 (1.9.293) — idempotency 중복 ${totalFixed}건 자동 정리 (task/user-request dedup)`, `🔁 --auto-fix on — deduped ${totalFixed} idempotency duplicate(s) (task/user-request)`));
|
|
246
|
+
}
|
|
247
|
+
} catch (e) {
|
|
248
|
+
afLog(t(`⚠ idempotency auto-fix 오류 (1.9.293): ${e.message}`, `⚠ idempotency auto-fix error: ${e.message}`));
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
// 1.9.236: drift check --auto-fix 에 release cleanup 통합 (1.9.235 회수)
|
|
252
|
+
// 누적된 50개+ release/* branches → abnormal-shutdown release-branch-pending 신호 가중
|
|
253
|
+
// 안전: keep 10 (최근 10개 유지), merged 만 삭제 (1.9.235 안전 가드)
|
|
254
|
+
// 임계: 50개 초과 시만 자동 정리 (소량 누적은 정상 운영)
|
|
255
|
+
if (autoFix) {
|
|
256
|
+
try {
|
|
257
|
+
const branchR = cp.spawnSync('git', ['branch', '--merged', 'main', '--list', 'release/*'], { cwd: root, encoding: 'utf8' });
|
|
258
|
+
if (branchR.status === 0) {
|
|
259
|
+
const merged = (branchR.stdout || '').split('\n')
|
|
260
|
+
.map(l => l.replace(/^\*?\s+/, '').trim())
|
|
261
|
+
.filter(l => l && /^release\/\d+\.\d+\.\d+$/.test(l));
|
|
262
|
+
if (merged.length > 50) {
|
|
263
|
+
afLog('');
|
|
264
|
+
afLog(t(`🗑 --auto-fix 활성 (1.9.236) — release/* merged ${merged.length}개 (50+) 자동 정리 (keep 10)...`, `🗑 --auto-fix on — cleaning up ${merged.length} merged release/* branches (50+, keep 10)...`));
|
|
265
|
+
// 정렬 (semver desc)
|
|
266
|
+
merged.sort((a, b) => {
|
|
267
|
+
const va = a.replace('release/', '').split('.').map(n => parseInt(n, 10) || 0);
|
|
268
|
+
const vb = b.replace('release/', '').split('.').map(n => parseInt(n, 10) || 0);
|
|
269
|
+
for (let i = 0; i < 3; i++) if (va[i] !== vb[i]) return vb[i] - va[i];
|
|
270
|
+
return 0;
|
|
271
|
+
});
|
|
272
|
+
const currentBranchR = cp.spawnSync('git', ['rev-parse', '--abbrev-ref', 'HEAD'], { cwd: root, encoding: 'utf8' });
|
|
273
|
+
const currentBranch = (currentBranchR.stdout || '').trim();
|
|
274
|
+
const toDelete = merged.slice(10).filter(b => b !== currentBranch);
|
|
275
|
+
let ok = 0;
|
|
276
|
+
for (const b of toDelete) {
|
|
277
|
+
const r = cp.spawnSync('git', ['branch', '-d', b], { cwd: root, encoding: 'utf8' });
|
|
278
|
+
if (r.status === 0) ok++;
|
|
279
|
+
}
|
|
280
|
+
afLog(t(`✓ release cleanup 자동 완료 ${ok}/${toDelete.length}건 (keep 10)`, `✓ release cleanup done ${ok}/${toDelete.length} (keep 10)`));
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
} catch (e) {
|
|
284
|
+
afLog(t(`⚠ release cleanup auto-fix 오류 (1.9.236): ${e.message}`, `⚠ release cleanup auto-fix error: ${e.message}`));
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
if (autoFix && level === '🔴 critical' && !hasSecurityFired) {
|
|
288
|
+
afLog('');
|
|
289
|
+
afLog(t(`🔧 --auto-fix 활성 — session close 자동 실행 중...`, `🔧 --auto-fix on — running session close...`));
|
|
290
|
+
try {
|
|
291
|
+
const r = cp.spawnSync(process.execPath, [harnessPath, 'session', 'close', root], { encoding: 'utf8', timeout: 60000, env: { ...process.env, LEERNESS_INTERNAL: '1' } });
|
|
292
|
+
if (r.status === 0) {
|
|
293
|
+
afLog(t(`✓ session close 자동 완료`, `✓ session close done`));
|
|
294
|
+
// autoResolved 카운트
|
|
295
|
+
const stats = _readUsageStats(root);
|
|
296
|
+
stats.drift = stats.drift || {};
|
|
297
|
+
stats.drift.autoResolved = (stats.drift.autoResolved || 0) + 1;
|
|
298
|
+
const p = _usageStatsPath(root);
|
|
299
|
+
mkdirp(path.dirname(p));
|
|
300
|
+
writeUtf8(p, JSON.stringify(stats, null, 2) + '\n');
|
|
301
|
+
// 재검사
|
|
302
|
+
afLog('');
|
|
303
|
+
afLog(t(`재검사 중...`, `re-checking...`));
|
|
304
|
+
return driftCheckCmd(root, { ...opts, _noAutoFix: true }, deps); // 재귀 1회 (auto-fix 없이, 1.9.432 depth 가드)
|
|
305
|
+
} else {
|
|
306
|
+
afLog(t(`⚠ session close 실패 (exit ${r.status}) — 수동 실행 필요`, `⚠ session close failed (exit ${r.status}) — run manually`));
|
|
307
|
+
}
|
|
308
|
+
} catch (e) {
|
|
309
|
+
afLog(t(`⚠ auto-fix 오류: ${e.message}`, `⚠ auto-fix error: ${e.message}`));
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
if (has('--json')) {
|
|
313
|
+
log(JSON.stringify({ root, score: totalScore, level, signals, fired, appsZeroTask }, null, 2));
|
|
314
|
+
return;
|
|
315
|
+
}
|
|
316
|
+
log(`# leerness drift check (1.9.37)`);
|
|
317
|
+
log(t(`경로: ${root}`, `path: ${root}`));
|
|
318
|
+
log('');
|
|
319
|
+
log(t(`상태: ${level} · 점수 ${totalScore}/200`, `status: ${level} · score ${totalScore}/200`));
|
|
320
|
+
log('');
|
|
321
|
+
log(t(`| 신호 | age | 임계 | 가중치 | 발화 |`, `| signal | age | threshold | weight | fired |`));
|
|
322
|
+
log(`|---|---:|---:|---:|---|`);
|
|
323
|
+
for (const s of signals) {
|
|
324
|
+
const fire = s.ageDays > s.threshold ? '🔥' : '✓';
|
|
325
|
+
const age = s.ageDays === null ? '-' : `${s.ageDays.toFixed(1)}d`;
|
|
326
|
+
log(`| ${s.label} | ${age} | ${s.threshold}d | ${s.weight} | ${fire} |`);
|
|
327
|
+
}
|
|
328
|
+
if (appsZeroTask.length) {
|
|
329
|
+
log('');
|
|
330
|
+
log(t(`task 0건 sub-app (${appsZeroTask.length}개): ${appsZeroTask.join(', ')}`, `sub-apps with 0 tasks (${appsZeroTask.length}): ${appsZeroTask.join(', ')}`));
|
|
331
|
+
}
|
|
332
|
+
if (totalScore >= 50) {
|
|
333
|
+
log('');
|
|
334
|
+
log(t(`💡 권장 조치:`, `💡 recommended actions:`));
|
|
335
|
+
log(t(` - 즉시: leerness session close . (handoff/current-state 갱신)`, ` - now: leerness session close . (refresh handoff/current-state)`));
|
|
336
|
+
log(t(` - 또는: leerness audit . --fix (자동 갱신 가능 항목 적용)`, ` - or: leerness audit . --fix (apply auto-fixable items)`));
|
|
337
|
+
log(t(` - sub-app에 task 등록: cd _apps/X && leerness task add "..."`, ` - add tasks to a sub-app: cd _apps/X && leerness task add "..."`));
|
|
338
|
+
log(t(` - 이 검사 끄기: --no-drift-check 또는 LEERNESS_NO_DRIFT_CHECK=1`, ` - disable this check: --no-drift-check or LEERNESS_NO_DRIFT_CHECK=1`));
|
|
339
|
+
}
|
|
340
|
+
if (level === '🔴 critical') process.exitCode = 1;
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
module.exports = { driftCheckCmd };
|