forked from CodebuffAI/codebuff
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-bot-sweep.ts
More file actions
71 lines (60 loc) · 2.17 KB
/
Copy pathtest-bot-sweep.ts
File metadata and controls
71 lines (60 loc) · 2.17 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
/**
* One-off runner to execute the bot-sweep pipeline directly (bypassing the
* HTTP endpoint) and email the result. Use this to exercise
* identifyBotSuspects + formatSweepReport + sendBasicEmail end-to-end before
* the GitHub Action is wired up.
*
* usage: infisical run --env=prod --path=/ -- bun scripts/test-bot-sweep.ts
*/
import { sendBasicEmail } from '@codebuff/internal/loops/client'
import {
formatSweepReport,
identifyBotSuspects,
} from '../web/src/server/free-session/abuse-detection'
import { reviewSuspects } from '../web/src/server/free-session/abuse-review'
const RECIPIENT = process.env.BOT_SWEEP_TEST_RECIPIENT ?? 'james@codebuff.com'
const logger = {
debug: (...args: any[]) => console.log('[debug]', ...args),
info: (...args: any[]) => console.log('[info]', ...args),
warn: (...args: any[]) => console.log('[warn]', ...args),
error: (...args: any[]) => console.log('[error]', ...args),
}
async function main() {
console.log('Running identifyBotSuspects…')
const report = await identifyBotSuspects({ logger })
const { subject, message } = formatSweepReport(report)
console.log('\n--- SUBJECT ---')
console.log(subject)
console.log('\n--- RULE-BASED BODY ---')
console.log(message)
console.log('\nRunning agent review (Claude Sonnet 4.6)…')
const agentReview = await reviewSuspects({ report, logger })
if (agentReview) {
console.log('\n--- AGENT REVIEW ---')
console.log(agentReview)
} else {
console.log('(agent review returned null — falling back to rule-only)')
}
console.log('\n--- END ---')
const fullMessage = agentReview
? `=== AGENT REVIEW (Claude Sonnet 4.6) ===\n\n${agentReview}\n\n=== RAW RULE-BASED DATA ===\n\n${message}`
: message
console.log(`\nSending email to ${RECIPIENT}…`)
const result = await sendBasicEmail({
email: RECIPIENT,
data: { subject, message: fullMessage },
logger,
})
if (result.success) {
console.log(`✅ Email sent (loopsId=${result.loopsId ?? 'n/a'})`)
} else {
console.error(`❌ Email failed: ${result.error}`)
process.exit(1)
}
}
main()
.then(() => process.exit(0))
.catch((err) => {
console.error(err)
process.exit(1)
})