Repository navigation
Expand file tree
/
Copy pathfirst-pass.test.mjs
More file actions
394 lines (363 loc) · 16.4 KB
/
Copy pathfirst-pass.test.mjs
File metadata and controls
394 lines (363 loc) · 16.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { spawn } from 'node:child_process';
import {
mkdtempSync,
mkdirSync,
rmSync,
symlinkSync,
writeFileSync,
} from 'node:fs';
import { createServer } from 'node:http';
import { tmpdir } from 'node:os';
import { join, resolve } from 'node:path';
import {
createRepoReadSession,
chooseFinalReply,
formatFirstPassFallback,
formatAutomatedComment,
hasTrustedFirstPass,
isAllowedRepoPath,
needsProFirstPass,
parseFirstPassResponse,
resolveFirstPassCandidates,
resolveGeneratorAttribution,
validateFirstPassEnvironment,
} from './first-pass-lib.mjs';
const ANTHROPIC_MESSAGES = 'anthropic-messages';
const REPO_ROOT = resolve(import.meta.dirname, '..', '..');
async function runFallbackScenario(eventKind, modelResponse = { content: [] }) {
const postedComments = [];
let modelCalls = 0;
const modelRequests = [];
const server = createServer(async (request, response) => {
if (request.method === 'POST' && request.url === '/v1/messages') {
modelCalls += 1;
const chunks = [];
for await (const chunk of request) chunks.push(chunk);
modelRequests.push(JSON.parse(Buffer.concat(chunks).toString('utf8')));
response.writeHead(modelResponse.status || 200, { 'content-type': 'application/json' });
response.end(JSON.stringify(modelResponse.body || modelResponse));
return;
}
if (request.method === 'GET' && request.url?.endsWith('/comments?per_page=100')) {
response.writeHead(200, { 'content-type': 'application/json' });
response.end('[]');
return;
}
if (request.method === 'POST' && request.url?.endsWith('/comments')) {
const chunks = [];
for await (const chunk of request) chunks.push(chunk);
postedComments.push(JSON.parse(Buffer.concat(chunks).toString('utf8')).body);
response.writeHead(201, { 'content-type': 'application/json' });
response.end('{}');
return;
}
response.writeHead(404);
response.end();
});
await new Promise((accept) => server.listen(0, '127.0.0.1', accept));
const address = server.address();
const apiUrl = `http://127.0.0.1:${address.port}`;
const fixtureRoot = mkdtempSync(join(tmpdir(), 'ccu-first-pass-'));
const diffFile = join(fixtureRoot, 'pr.diff');
writeFileSync(diffFile, 'diff --git a/src/a.ts b/src/a.ts\n+export const value = 1;\n', 'utf8');
try {
const child = spawn(process.execPath, ['.github/scripts/first-pass.mjs'], {
cwd: REPO_ROOT,
env: {
...process.env,
GH_TOKEN: 'test-github-token',
GITHUB_API_URL: apiUrl,
REPO: 'ClaudeCodeUsage/ClaudeCodeUsage',
EVENT_KIND: eventKind,
ITEM_NUMBER: eventKind === 'issue' ? '87' : '88',
ITEM_TITLE: eventKind === 'issue' ? 'High CPU usage' : 'Reduce background work',
ITEM_BODY: 'Reproduction details and tests are included.',
DIFF_FILE: eventKind === 'pr' ? diffFile : '',
ANTHROPIC_API_KEY: 'test-model-key',
ANTHROPIC_BASE_URL: apiUrl,
CCU_BOT_MODEL: 'deepseek-v4-flash',
CCU_BOT_MODEL_PRO: 'deepseek-v4-pro',
CCU_BOT_GENERATOR: 'deepseek',
CCU_BOT_GENERATOR_PRO: 'deepseek',
},
stdio: ['ignore', 'pipe', 'pipe'],
});
let stdout = '';
let stderr = '';
child.stdout.setEncoding('utf8');
child.stderr.setEncoding('utf8');
child.stdout.on('data', (chunk) => { stdout += chunk; });
child.stderr.on('data', (chunk) => { stderr += chunk; });
const exitCode = await new Promise((accept) => child.on('close', accept));
return { exitCode, modelCalls, modelRequests, postedComments, stdout, stderr };
} finally {
rmSync(fixtureRoot, { recursive: true, force: true });
await new Promise((accept) => server.close(accept));
}
}
test('issue runner retains its fallback, but failed PR review posts nothing and fails visibly', async () => {
const issue = await runFallbackScenario('issue');
assert.equal(issue.exitCode, 0, issue.stderr);
assert.equal(issue.modelCalls, 2);
assert.match(issue.postedComments[0], /^🤖 Automated first-pass reply/);
const pr = await runFallbackScenario('pr');
assert.notEqual(pr.exitCode, 0);
assert.equal(pr.modelCalls, 2);
assert.equal(pr.postedComments.length, 0);
assert.match(pr.stderr, /cheap=empty-reply.*pro=empty-reply/);
assert.ok(pr.modelRequests[1].max_tokens > 6000);
assert.equal(pr.modelRequests[1].thinking?.type, 'enabled');
assert.doesNotMatch(pr.stderr, /test-model-key|test-github-token/);
});
test('API failures report status and tier, never remote body or credentials', async () => {
const result = await runFallbackScenario('pr', {
status: 401,
body: { error: 'sensitive untrusted upstream response' },
});
assert.notEqual(result.exitCode, 0);
assert.equal(result.postedComments.length, 0);
assert.match(result.stderr, /cheap=http-401.*pro=http-401/);
assert.doesNotMatch(result.stderr, /sensitive untrusted|test-model-key|test-github-token/);
});
test('DeepSeek tiers have compatible output budgets and reserve the cheap tier for a reply', async () => {
const result = await runFallbackScenario('pr', {
content: [{ type: 'text', text: '<control>{"answerable":true}</control><reply>Specific finding in src/a.ts.</reply>' }],
});
assert.equal(result.exitCode, 0, result.stderr);
assert.equal(result.modelCalls, 1);
assert.equal(result.modelRequests[0].thinking?.type, 'disabled');
assert.ok(result.modelRequests[0].max_tokens >= 1400);
assert.match(result.postedComments[0], /Specific finding in src\/a\.ts/);
});
test('empty cheap replies escalate even when the control says answerable', () => {
assert.equal(needsProFirstPass({ answerable: true, reply: '' }), true);
assert.equal(needsProFirstPass({ answerable: true, reply: 'usable' }), false);
assert.equal(needsProFirstPass({ answerable: false, reply: 'needs source' }), true);
});
test('static fallback is honest, English-first, and names no model provider', () => {
const english = formatFirstPassFallback({ kind: 'reply', isChinese: false });
assert.match(english, /Automated first-pass reply/);
assert.match(english, /did not return a usable analysis/);
assert.match(english, /Posted automatically by CCU Bot/);
assert.doesNotMatch(english, /DeepSeek|Claude|OpenAI Codex|Generated (?:with|by)/);
const bilingual = formatFirstPassFallback({ kind: 'reply', isChinese: true });
assert.ok(bilingual.indexOf('did not return a usable analysis') < bilingual.indexOf('自动分析服务'));
});
test('only a trusted GitHub Actions author and wrapper marker suppress a duplicate', () => {
const trustedBody = '🤖 Automated first-pass reply\n\nUseful body';
assert.equal(hasTrustedFirstPass([
{ user: { login: 'github-actions[bot]' }, body: trustedBody },
], 'reply'), true);
assert.equal(hasTrustedFirstPass([
{ user: { login: 'someone' }, body: trustedBody },
], 'reply'), false);
assert.equal(hasTrustedFirstPass([
{ user: { login: 'github-actions[bot]' }, body: 'ordinary comment' },
], 'reply'), false);
});
test('candidate resolution calls the pro tier when the cheap reply is empty', async () => {
const cheapGenerator = { id: 'cheap' };
const proGenerator = { id: 'pro' };
let proCalls = 0;
const selected = await resolveFirstPassCandidates({
cheap: async () => ({ reply: '', answerable: true, generator: cheapGenerator }),
pro: async () => {
proCalls += 1;
return { reply: 'grounded pro reply', answerable: true, generator: proGenerator };
},
});
assert.equal(proCalls, 1);
assert.deepEqual(selected, { reply: 'grounded pro reply', generator: proGenerator });
});
test('candidate resolution returns null instead of throwing when both tiers fail', async () => {
let proCalls = 0;
const selected = await resolveFirstPassCandidates({
cheap: async () => { throw new Error('temporary cheap failure'); },
pro: async () => {
proCalls += 1;
return { reply: '', answerable: true, generator: { id: 'pro' } };
},
});
assert.equal(proCalls, 1);
assert.equal(selected, null);
});
test('an unanswered cheap draft is not posted when the pro tier fails', async () => {
const failures = [];
const selected = await resolveFirstPassCandidates({
cheap: async () => ({ reply: 'Need source', answerable: false }),
pro: async () => { throw new Error('secret remote text'); },
onFailure: (event) => failures.push(event),
});
assert.equal(selected, null);
assert.deepEqual(failures, [
{ tier: 'cheap', reason: 'needs-source' },
{ tier: 'pro', reason: 'request-error' },
]);
});
test('current transport truthfully supports DeepSeek and Claude per tier', () => {
const cheap = resolveGeneratorAttribution(undefined, 'deepseek-v4-flash', ANTHROPIC_MESSAGES);
const pro = resolveGeneratorAttribution('claude', 'claude-sonnet-4-5', ANTHROPIC_MESSAGES);
assert.equal(cheap.id, 'deepseek');
assert.equal(pro.id, 'claude');
assert.match(
formatAutomatedComment('cheap body', { kind: 'reply', generator: cheap }),
/Generated by \[DeepSeek\]\(https:\/\/www\.deepseek\.com\/\)/,
);
assert.match(
formatAutomatedComment('pro body', { kind: 'review', generator: pro }),
/Generated by \[Claude\]\(https:\/\/www\.anthropic\.com\/claude\)/,
);
});
test('a configured provider must match that tier model', () => {
assert.throws(
() => resolveGeneratorAttribution('deepseek', 'claude-sonnet-4-5', ANTHROPIC_MESSAGES),
/does not match configured generator deepseek/,
);
assert.throws(
() => resolveGeneratorAttribution(undefined, 'third-party-mystery-model', ANTHROPIC_MESSAGES),
/Cannot infer generator/,
);
});
test('Codex automatic attribution fails closed on the current transport', () => {
assert.throws(
() => resolveGeneratorAttribution('codex', 'gpt-5.3-codex', ANTHROPIC_MESSAGES),
/requires transport openai-responses-codex/,
);
});
test('future Codex attribution requires its trusted transport contract', () => {
const future = resolveGeneratorAttribution(
'codex',
'gpt-5.3-codex',
'openai-responses-codex',
);
assert.equal(future.id, 'codex');
assert.match(future.automaticFooter, /Generated by \[OpenAI Codex\]/);
});
test('trusted wrapper strips spoofed markdown variants and appends one exact suffix', () => {
const generator = resolveGeneratorAttribution(undefined, 'deepseek-v4-flash', ANTHROPIC_MESSAGES);
const untrusted = [
'> 🤖 AUTOMATED FIRST-PASS REVIEW (via Claude Code)',
'',
'Useful body',
'',
'- 🤖 Generated with [OpenAI Codex](https://evil.invalid/codex)',
'```md',
'🤖 generated BY DeepSeek as an automated first pass',
'```',
'1. This is model-generated — not a maintainer decision.',
].join('\n');
const comment = formatAutomatedComment(untrusted, { kind: 'review', generator });
const suffix = [
'---',
'🤖 Generated by [DeepSeek](https://www.deepseek.com/) as an automated first pass — not a maintainer decision.',
].join('\n');
assert.match(comment, /Useful body/);
assert.equal(comment.endsWith(suffix), true);
assert.equal((comment.match(/Generated\s+(?:with|by)/giu) ?? []).length, 1);
assert.equal((comment.match(/Automated first-pass review/giu) ?? []).length, 1);
assert.doesNotMatch(comment, /evil\.invalid|Claude Code|OpenAI Codex/iu);
});
test('preflight rejects missing credentials and ungrounded PRs', () => {
const base = {
GH_TOKEN: 'github-token', REPO: 'ClaudeCodeUsage/ClaudeCodeUsage',
EVENT_KIND: 'issue', ITEM_NUMBER: '70', ANTHROPIC_API_KEY: 'model-key',
};
assert.equal(validateFirstPassEnvironment(base).isPr, false);
assert.throws(
() => validateFirstPassEnvironment({ ...base, ANTHROPIC_API_KEY: '' }),
/Missing required environment variables: ANTHROPIC_API_KEY/,
);
assert.throws(
() => validateFirstPassEnvironment({ ...base, EVENT_KIND: 'pr' }),
/DIFF_FILE is required for pull requests/,
);
});
test('only explicit root docs/config and src are readable', () => {
for (const allowed of [
'AGENTS.md', 'ARCHITECTURE.md', 'CLAUDE.md', 'CONTRIBUTING.md',
'CHANGELOG.md', 'package.json', 'tsconfig.json', 'src/dataLoader.ts',
]) assert.equal(isAllowedRepoPath(allowed), true, allowed);
for (const denied of [
'../outside.md', '/etc/passwd', 'C:\\Users\\person\\auth.json',
'.env', '.env.local', '.github/workflows/publish.yml',
'.claude/.credentials.json', 'docs/private.md', 'test/fixture.json',
'src/.env.production', 'src/nested/oauth.json',
'src/nested/credentials.json', 'src/nested/secrets.json',
'src/nested/private.pem', 'src/nested/token.txt', 'src/code.bin',
]) assert.equal(isAllowedRepoPath(denied), false, denied);
});
test('repo reader shares stable-dedup, byte budgets and symlink denial for the run', (t) => {
const repoRoot = mkdtempSync(join(tmpdir(), 'ccu-reader-'));
const outsideRoot = mkdtempSync(join(tmpdir(), 'ccu-reader-outside-'));
t.after(() => {
rmSync(repoRoot, { recursive: true, force: true });
rmSync(outsideRoot, { recursive: true, force: true });
});
mkdirSync(join(repoRoot, 'src', 'real-dir'), { recursive: true });
for (const name of ['AGENTS.md', 'ARCHITECTURE.md', 'CLAUDE.md', 'CONTRIBUTING.md']) {
writeFileSync(join(repoRoot, name), `${name}\n`, 'utf8');
}
for (const name of ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']) {
writeFileSync(join(repoRoot, 'src', `${name}.txt`), `${name}-body\n`, 'utf8');
}
writeFileSync(join(repoRoot, 'src', 'huge.txt'), 'x'.repeat(20_000), 'utf8');
writeFileSync(join(repoRoot, 'src', 'unicode.txt'), '中文🙂'.repeat(8_000), 'utf8');
writeFileSync(join(repoRoot, 'src', 'real-dir', 'inside.txt'), 'inside regular', 'utf8');
writeFileSync(join(outsideRoot, 'secret.txt'), 'outside secret', 'utf8');
symlinkSync(join(outsideRoot, 'secret.txt'), join(repoRoot, 'src', 'link.txt'));
symlinkSync(outsideRoot, join(repoRoot, 'src', 'external-dir'));
symlinkSync(join(repoRoot, 'src', 'real-dir'), join(repoRoot, 'src', 'internal-link'));
const ordering = createRepoReadSession({ repoRoot });
const ordered = ordering.read([
'src/a.txt', 'src/a.txt', 'src/b.txt', 'src/c.txt', 'src/d.txt',
'src/e.txt', 'src/f.txt', 'src/g.txt', 'src/link.txt',
]);
assert.deepEqual(
ordered.files.map((file) => file.path),
['src/a.txt', 'src/b.txt', 'src/c.txt', 'src/d.txt', 'src/e.txt', 'src/f.txt'],
);
const capped = createRepoReadSession({ repoRoot });
const result = capped.read([
'src/huge.txt', 'src/unicode.txt', 'src/a.txt', 'src/b.txt',
'src/c.txt', 'src/d.txt', 'src/link.txt',
'src/external-dir/secret.txt', 'src/internal-link/inside.txt',
'src/nested/oauth.json', '../outside.md',
]);
assert.ok(result.files.every((file) => file.bytes <= 16_000));
assert.ok(result.totalBytes <= 60_000);
assert.equal(result.files.find((file) => file.path === 'src/huge.txt')?.bytes, 16_000);
assert.equal(result.files.some((file) => file.text.endsWith('\uFFFD')), false);
assert.equal(result.text.includes('outside secret'), false);
assert.equal(result.files.some((file) => /link|oauth|outside/.test(file.path)), false);
const perRun = createRepoReadSession({ repoRoot });
const grounding = perRun.read(['AGENTS.md', 'ARCHITECTURE.md', 'CLAUDE.md', 'CONTRIBUTING.md']);
const requested = perRun.read([
'AGENTS.md',
'src/a.txt', 'src/b.txt', 'src/c.txt', 'src/d.txt', 'src/e.txt', 'src/f.txt',
]);
assert.equal(grounding.files.length, 4);
assert.ok(requested.files.length <= 2);
assert.ok(perRun.snapshot().filesRead <= 6);
assert.ok(perRun.snapshot().totalBytes <= 60_000);
});
test('empty tagged pro reply preserves the cheap body and attribution', () => {
const cheap = resolveGeneratorAttribution('deepseek', 'deepseek-v4-flash', 'anthropic-messages');
const pro = resolveGeneratorAttribution('claude', 'claude-sonnet-4-6', 'anthropic-messages');
const parsedPro = parseFirstPassResponse('<control>{"answerable":true}</control><reply> </reply>');
assert.equal(parsedPro.reply, '');
const selected = chooseFinalReply({ reply: 'cheap body', generator: cheap }, {
reply: parsedPro.reply,
generator: pro,
});
assert.equal(selected.reply, 'cheap body');
assert.equal(selected.generator.id, 'deepseek');
});
test('no non-empty tier can never reach the GitHub formatter', () => {
const cheap = resolveGeneratorAttribution('deepseek', 'deepseek-v4-flash', 'anthropic-messages');
assert.throws(
() => chooseFinalReply({ reply: ' ', generator: cheap }),
/No non-empty model reply/,
);
});