@kaitranntt/ccs 7.63.0 → 7.63.1
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/package.json
CHANGED
|
@@ -0,0 +1,328 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { fileURLToPath } from 'node:url';
|
|
4
|
+
|
|
5
|
+
const ASSESSMENTS = {
|
|
6
|
+
approved: '✅ APPROVED',
|
|
7
|
+
approved_with_notes: '⚠️ APPROVED WITH NOTES',
|
|
8
|
+
changes_requested: '❌ CHANGES REQUESTED',
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
const SEVERITY_ORDER = ['high', 'medium', 'low'];
|
|
12
|
+
const SEVERITY_HEADERS = {
|
|
13
|
+
high: '### 🔴 High',
|
|
14
|
+
medium: '### 🟡 Medium',
|
|
15
|
+
low: '### 🟢 Low',
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
const STATUS_LABELS = {
|
|
19
|
+
pass: '✅',
|
|
20
|
+
fail: '⚠️',
|
|
21
|
+
na: 'N/A',
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
const RENDERER_OWNED_MARKUP_PATTERNS = [
|
|
25
|
+
{ pattern: /^#{1,6}\s/u, reason: 'markdown heading' },
|
|
26
|
+
{ pattern: /^\s*Verdict\s*:/iu, reason: 'verdict label' },
|
|
27
|
+
{ pattern: /^\s*PR\s*#?\d+\s*Review(?:\s*[:.-]|$)/iu, reason: 'ad hoc PR heading' },
|
|
28
|
+
{ pattern: /\|\s*[-:]+\s*\|/u, reason: 'markdown table' },
|
|
29
|
+
{ pattern: /```/u, reason: 'code fence' },
|
|
30
|
+
];
|
|
31
|
+
|
|
32
|
+
function cleanText(value) {
|
|
33
|
+
return typeof value === 'string' ? value.trim().replace(/\s+/g, ' ') : '';
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function escapeMarkdownText(value) {
|
|
37
|
+
return cleanText(value).replace(/\\/g, '\\\\').replace(/([`*_{}[\]<>|])/g, '\\$1');
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function renderCode(value) {
|
|
41
|
+
const text = cleanText(value);
|
|
42
|
+
const longestFence = Math.max(...[...text.matchAll(/`+/g)].map((match) => match[0].length), 0);
|
|
43
|
+
const fence = '`'.repeat(longestFence + 1);
|
|
44
|
+
return `${fence}${text}${fence}`;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function validatePlainTextField(fieldName, value) {
|
|
48
|
+
const text = cleanText(value);
|
|
49
|
+
if (!text) {
|
|
50
|
+
return { ok: false, reason: `${fieldName} is required` };
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const match = RENDERER_OWNED_MARKUP_PATTERNS.find(({ pattern }) => pattern.test(text));
|
|
54
|
+
if (match) {
|
|
55
|
+
return { ok: false, reason: `${fieldName} contains ${match.reason}` };
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
return { ok: true, value: text };
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function normalizeStringList(fieldName, raw) {
|
|
62
|
+
if (!Array.isArray(raw)) {
|
|
63
|
+
return { ok: false, reason: `${fieldName} must be an array` };
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const values = [];
|
|
67
|
+
for (const [index, item] of raw.entries()) {
|
|
68
|
+
const validation = validatePlainTextField(`${fieldName}[${index}]`, item);
|
|
69
|
+
if (!validation.ok) return validation;
|
|
70
|
+
values.push(validation.value);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
return { ok: true, value: values };
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function normalizeChecklistRows(fieldName, labelField, raw) {
|
|
77
|
+
if (!Array.isArray(raw)) {
|
|
78
|
+
return { ok: false, reason: `${fieldName} must be an array` };
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const rows = [];
|
|
82
|
+
for (const [index, item] of raw.entries()) {
|
|
83
|
+
const label = validatePlainTextField(`${fieldName}[${index}].${labelField}`, item?.[labelField]);
|
|
84
|
+
if (!label.ok) return label;
|
|
85
|
+
|
|
86
|
+
const notes = validatePlainTextField(`${fieldName}[${index}].notes`, item?.notes);
|
|
87
|
+
if (!notes.ok) return notes;
|
|
88
|
+
|
|
89
|
+
const status = cleanText(item?.status).toLowerCase();
|
|
90
|
+
if (!STATUS_LABELS[status]) {
|
|
91
|
+
return { ok: false, reason: `${fieldName}[${index}].status is invalid` };
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
rows.push({ [labelField]: label.value, status, notes: notes.value });
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
if (rows.length === 0) {
|
|
98
|
+
return { ok: false, reason: `${fieldName} must contain at least 1 item` };
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
return { ok: true, value: rows };
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function readExecutionMetadata(executionFile) {
|
|
105
|
+
if (!executionFile || !fs.existsSync(executionFile)) {
|
|
106
|
+
return {};
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
try {
|
|
110
|
+
const turns = JSON.parse(fs.readFileSync(executionFile, 'utf8'));
|
|
111
|
+
const init = turns.find((turn) => turn?.type === 'system' && turn?.subtype === 'init');
|
|
112
|
+
const result = [...turns].reverse().find((turn) => turn?.type === 'result');
|
|
113
|
+
return {
|
|
114
|
+
runtimeTools: Array.isArray(init?.tools) ? init.tools : [],
|
|
115
|
+
turnsUsed: typeof result?.num_turns === 'number' ? result.num_turns : null,
|
|
116
|
+
};
|
|
117
|
+
} catch {
|
|
118
|
+
return {};
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
export function normalizeStructuredOutput(raw) {
|
|
123
|
+
if (!raw) {
|
|
124
|
+
return { ok: false, reason: 'missing structured output' };
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
let parsed;
|
|
128
|
+
try {
|
|
129
|
+
parsed = typeof raw === 'string' ? JSON.parse(raw) : raw;
|
|
130
|
+
} catch {
|
|
131
|
+
return { ok: false, reason: 'structured output is not valid JSON' };
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) {
|
|
135
|
+
return { ok: false, reason: 'structured output must be an object' };
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
const summary = validatePlainTextField('summary', parsed.summary);
|
|
139
|
+
if (!summary.ok) return summary;
|
|
140
|
+
|
|
141
|
+
const overallAssessment = cleanText(parsed.overallAssessment).toLowerCase();
|
|
142
|
+
const overallRationale = validatePlainTextField('overallRationale', parsed.overallRationale);
|
|
143
|
+
if (!overallRationale.ok) return overallRationale;
|
|
144
|
+
|
|
145
|
+
const findings = Array.isArray(parsed.findings) ? parsed.findings : null;
|
|
146
|
+
const securityChecklist = normalizeChecklistRows('securityChecklist', 'check', parsed.securityChecklist);
|
|
147
|
+
if (!securityChecklist.ok) return securityChecklist;
|
|
148
|
+
|
|
149
|
+
const ccsCompliance = normalizeChecklistRows('ccsCompliance', 'rule', parsed.ccsCompliance);
|
|
150
|
+
if (!ccsCompliance.ok) return ccsCompliance;
|
|
151
|
+
|
|
152
|
+
const informational = normalizeStringList('informational', parsed.informational);
|
|
153
|
+
if (!informational.ok) return informational;
|
|
154
|
+
|
|
155
|
+
const strengths = normalizeStringList('strengths', parsed.strengths);
|
|
156
|
+
if (!strengths.ok) return strengths;
|
|
157
|
+
|
|
158
|
+
if (!ASSESSMENTS[overallAssessment] || findings === null) {
|
|
159
|
+
return { ok: false, reason: 'structured output is missing required review fields' };
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
const normalizedFindings = [];
|
|
163
|
+
for (const [index, finding] of findings.entries()) {
|
|
164
|
+
const severity = cleanText(finding?.severity).toLowerCase();
|
|
165
|
+
const title = validatePlainTextField(`findings[${index}].title`, finding?.title);
|
|
166
|
+
if (!title.ok) return title;
|
|
167
|
+
|
|
168
|
+
const file = validatePlainTextField(`findings[${index}].file`, finding?.file);
|
|
169
|
+
if (!file.ok) return file;
|
|
170
|
+
|
|
171
|
+
const what = validatePlainTextField(`findings[${index}].what`, finding?.what);
|
|
172
|
+
if (!what.ok) return what;
|
|
173
|
+
|
|
174
|
+
const why = validatePlainTextField(`findings[${index}].why`, finding?.why);
|
|
175
|
+
if (!why.ok) return why;
|
|
176
|
+
|
|
177
|
+
const fix = validatePlainTextField(`findings[${index}].fix`, finding?.fix);
|
|
178
|
+
if (!fix.ok) return fix;
|
|
179
|
+
|
|
180
|
+
let line = null;
|
|
181
|
+
if (finding && Object.hasOwn(finding, 'line')) {
|
|
182
|
+
if (finding.line === null) {
|
|
183
|
+
line = null;
|
|
184
|
+
} else if (typeof finding.line === 'number' && Number.isInteger(finding.line) && finding.line > 0) {
|
|
185
|
+
line = finding.line;
|
|
186
|
+
} else {
|
|
187
|
+
return { ok: false, reason: `findings[${index}].line is invalid` };
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
if (!SEVERITY_HEADERS[severity]) {
|
|
192
|
+
return { ok: false, reason: `findings[${index}].severity is invalid` };
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
normalizedFindings.push({
|
|
196
|
+
severity,
|
|
197
|
+
title: title.value,
|
|
198
|
+
file: file.value,
|
|
199
|
+
line,
|
|
200
|
+
what: what.value,
|
|
201
|
+
why: why.value,
|
|
202
|
+
fix: fix.value,
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
return {
|
|
207
|
+
ok: true,
|
|
208
|
+
value: {
|
|
209
|
+
summary: summary.value,
|
|
210
|
+
findings: normalizedFindings,
|
|
211
|
+
overallAssessment,
|
|
212
|
+
overallRationale: overallRationale.value,
|
|
213
|
+
securityChecklist: securityChecklist.value,
|
|
214
|
+
ccsCompliance: ccsCompliance.value,
|
|
215
|
+
informational: informational.value,
|
|
216
|
+
strengths: strengths.value,
|
|
217
|
+
},
|
|
218
|
+
};
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
function renderChecklistTable(title, labelHeader, labelKey, rows) {
|
|
222
|
+
const lines = ['', title, '', `| ${labelHeader} | Status | Notes |`, '|---|---|---|'];
|
|
223
|
+
for (const row of rows) {
|
|
224
|
+
lines.push(
|
|
225
|
+
`| ${escapeMarkdownText(row[labelKey])} | ${STATUS_LABELS[row.status]} | ${escapeMarkdownText(row.notes)} |`
|
|
226
|
+
);
|
|
227
|
+
}
|
|
228
|
+
return lines;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
function renderBulletSection(title, items) {
|
|
232
|
+
if (items.length === 0) return [];
|
|
233
|
+
return ['', title, ...items.map((item) => `- ${escapeMarkdownText(item)}`)];
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
export function renderStructuredReview(review, { model }) {
|
|
237
|
+
const lines = ['### 📋 Summary', '', escapeMarkdownText(review.summary), '', '### 🔍 Findings'];
|
|
238
|
+
|
|
239
|
+
if (review.findings.length === 0) {
|
|
240
|
+
lines.push('No confirmed issues found after reviewing the diff and surrounding code.');
|
|
241
|
+
} else {
|
|
242
|
+
for (const severity of SEVERITY_ORDER) {
|
|
243
|
+
const findings = review.findings.filter((finding) => finding.severity === severity);
|
|
244
|
+
if (findings.length === 0) continue;
|
|
245
|
+
|
|
246
|
+
lines.push('', SEVERITY_HEADERS[severity], '');
|
|
247
|
+
for (const finding of findings) {
|
|
248
|
+
const location = finding.line ? `${finding.file}:${finding.line}` : finding.file;
|
|
249
|
+
lines.push(`- **${renderCode(location)} — ${escapeMarkdownText(finding.title)}**`);
|
|
250
|
+
lines.push(` Problem: ${escapeMarkdownText(finding.what)}`);
|
|
251
|
+
lines.push(` Why it matters: ${escapeMarkdownText(finding.why)}`);
|
|
252
|
+
lines.push(` Suggested fix: ${escapeMarkdownText(finding.fix)}`);
|
|
253
|
+
lines.push('');
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
if (lines[lines.length - 1] === '') lines.pop();
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
lines.push(...renderChecklistTable('### 🔒 Security Checklist', 'Check', 'check', review.securityChecklist));
|
|
260
|
+
lines.push(...renderChecklistTable('### 📊 CCS Compliance', 'Rule', 'rule', review.ccsCompliance));
|
|
261
|
+
lines.push(...renderBulletSection('### 💡 Informational', review.informational));
|
|
262
|
+
lines.push(...renderBulletSection("### ✅ What's Done Well", review.strengths));
|
|
263
|
+
|
|
264
|
+
lines.push(
|
|
265
|
+
'',
|
|
266
|
+
'### 🎯 Overall Assessment',
|
|
267
|
+
'',
|
|
268
|
+
`**${ASSESSMENTS[review.overallAssessment]}** — ${escapeMarkdownText(review.overallRationale)}`,
|
|
269
|
+
'',
|
|
270
|
+
`> 🤖 Reviewed by \`${model}\``
|
|
271
|
+
);
|
|
272
|
+
|
|
273
|
+
return lines.join('\n');
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
export function renderIncompleteReview({ model, reason, runUrl, runtimeTools, turnsUsed }) {
|
|
277
|
+
const lines = [
|
|
278
|
+
'### ⚠️ AI Review Incomplete',
|
|
279
|
+
'',
|
|
280
|
+
'Claude did not return validated structured review output, so this workflow did not publish raw scratch text.',
|
|
281
|
+
'',
|
|
282
|
+
`- Reason: ${escapeMarkdownText(reason)}`,
|
|
283
|
+
];
|
|
284
|
+
|
|
285
|
+
if (runtimeTools?.length) {
|
|
286
|
+
lines.push(`- Runtime tools: ${runtimeTools.map(renderCode).join(', ')}`);
|
|
287
|
+
}
|
|
288
|
+
if (typeof turnsUsed === 'number') {
|
|
289
|
+
lines.push(`- Turns used: ${turnsUsed}`);
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
lines.push('', `Re-run \`/review\` or inspect [the workflow run](${runUrl}).`, '', `> 🤖 Reviewed by \`${model}\``);
|
|
293
|
+
return lines.join('\n');
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
export function writeReviewFromEnv(env = process.env) {
|
|
297
|
+
const outputFile = env.AI_REVIEW_OUTPUT_FILE || 'pr_review.md';
|
|
298
|
+
const model = env.AI_REVIEW_MODEL || 'unknown-model';
|
|
299
|
+
const runUrl = env.AI_REVIEW_RUN_URL || '#';
|
|
300
|
+
const validation = normalizeStructuredOutput(env.AI_REVIEW_STRUCTURED_OUTPUT);
|
|
301
|
+
const metadata = readExecutionMetadata(env.AI_REVIEW_EXECUTION_FILE);
|
|
302
|
+
const content = validation.ok
|
|
303
|
+
? renderStructuredReview(validation.value, { model })
|
|
304
|
+
: renderIncompleteReview({
|
|
305
|
+
model,
|
|
306
|
+
reason: validation.reason,
|
|
307
|
+
runUrl,
|
|
308
|
+
runtimeTools: metadata.runtimeTools,
|
|
309
|
+
turnsUsed: metadata.turnsUsed,
|
|
310
|
+
});
|
|
311
|
+
|
|
312
|
+
fs.mkdirSync(path.dirname(outputFile), { recursive: true });
|
|
313
|
+
fs.writeFileSync(outputFile, `${content}\n`, 'utf8');
|
|
314
|
+
|
|
315
|
+
if (!validation.ok) {
|
|
316
|
+
console.warn(`::warning::AI review output normalization fell back to incomplete comment: ${validation.reason}`);
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
return { usedFallback: !validation.ok, content };
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
const isMain =
|
|
323
|
+
process.argv[1] &&
|
|
324
|
+
path.resolve(process.argv[1]) === path.resolve(fileURLToPath(import.meta.url));
|
|
325
|
+
|
|
326
|
+
if (isMain) {
|
|
327
|
+
writeReviewFromEnv();
|
|
328
|
+
}
|