Skip to content

Commit 284d89c

Browse files
committed
resolve review agent codeql path alert properly
1 parent ddea515 commit 284d89c

4 files changed

Lines changed: 25 additions & 31 deletions

File tree

packages/web/src/features/agents/review-agent/app.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Octokit } from "octokit";
22
import { generatePrReviews } from "@/features/agents/review-agent/nodes/generatePrReview";
33
import { githubPushPrReviews } from "@/features/agents/review-agent/nodes/githubPushPrReviews";
44
import { githubPrParser } from "@/features/agents/review-agent/nodes/githubPrParser";
5-
import { getReviewAgentLogDir } from "@/features/agents/review-agent/nodes/invokeDiffReviewLlm";
5+
import { REVIEW_AGENT_LOG_DIR } from "@/features/agents/review-agent/lib";
66
import { env } from "@sourcebot/shared";
77
import { GitHubPullRequest } from "@/features/agents/review-agent/types";
88
import path from "path";
@@ -29,11 +29,10 @@ export async function processGitHubPullRequest(octokit: Octokit, pullRequest: Gi
2929
return;
3030
}
3131

32-
let reviewAgentLogPath: string | undefined;
32+
let reviewAgentLogFileName: string | undefined;
3333
if (env.REVIEW_AGENT_LOGGING_ENABLED) {
34-
const reviewAgentLogDir = getReviewAgentLogDir();
35-
if (!fs.existsSync(reviewAgentLogDir)) {
36-
fs.mkdirSync(reviewAgentLogDir, { recursive: true });
34+
if (!fs.existsSync(REVIEW_AGENT_LOG_DIR)) {
35+
fs.mkdirSync(REVIEW_AGENT_LOG_DIR, { recursive: true });
3736
}
3837

3938
const timestamp = new Date().toLocaleString('en-US', {
@@ -45,11 +44,12 @@ export async function processGitHubPullRequest(octokit: Octokit, pullRequest: Gi
4544
second: '2-digit',
4645
hour12: false
4746
}).replace(/(\d+)\/(\d+)\/(\d+), (\d+):(\d+):(\d+)/, '$3_$1_$2_$4_$5_$6');
48-
reviewAgentLogPath = path.join(reviewAgentLogDir, `review-agent-${pullRequest.number}-${timestamp}.log`);
49-
logger.info(`Review agent logging to ${reviewAgentLogPath}`);
47+
reviewAgentLogFileName = `review-agent-${pullRequest.number}-${timestamp}.log`;
48+
logger.info(`Review agent logging to ${path.join(REVIEW_AGENT_LOG_DIR, reviewAgentLogFileName)}`);
49+
5050
}
5151

5252
const prPayload = await githubPrParser(octokit, pullRequest);
53-
const fileDiffReviews = await generatePrReviews(reviewAgentLogPath, prPayload, rules);
53+
const fileDiffReviews = await generatePrReviews(reviewAgentLogFileName, prPayload, rules);
5454
await githubPushPrReviews(octokit, prPayload, fileDiffReviews);
5555
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import path from "path";
2+
import { env } from "@sourcebot/shared";
3+
import fs from "fs";
4+
5+
export const REVIEW_AGENT_LOG_DIR = env.DATA_CACHE_DIR + "/review-agent";
6+
7+
export const appendReviewAgentLog = (logFileName: string, log: string): void => {
8+
fs.appendFileSync(path.join(REVIEW_AGENT_LOG_DIR, logFileName), log);
9+
};

packages/web/src/features/agents/review-agent/nodes/generatePrReview.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { createLogger } from "@sourcebot/shared";
66

77
const logger = createLogger('generate-pr-review');
88

9-
export const generatePrReviews = async (reviewAgentLogPath: string | undefined, pr_payload: sourcebot_pr_payload, rules: string[]): Promise<sourcebot_file_diff_review[]> => {
9+
export const generatePrReviews = async (reviewAgentLogFileName: string | undefined, pr_payload: sourcebot_pr_payload, rules: string[]): Promise<sourcebot_file_diff_review[]> => {
1010
logger.debug("Executing generate_pr_reviews");
1111

1212
const file_diff_reviews: sourcebot_file_diff_review[] = [];
@@ -32,7 +32,7 @@ export const generatePrReviews = async (reviewAgentLogPath: string | undefined,
3232

3333
const prompt = await generateDiffReviewPrompt(diff, context, rules);
3434

35-
const diffReview = await invokeDiffReviewLlm(reviewAgentLogPath, prompt);
35+
const diffReview = await invokeDiffReviewLlm(reviewAgentLogFileName, prompt);
3636
reviews.push(...diffReview.reviews);
3737
} catch (error) {
3838
logger.error(`Error generating review for ${file_diff.to}: ${error}`);

packages/web/src/features/agents/review-agent/nodes/invokeDiffReviewLlm.ts

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,12 @@
11
import OpenAI from "openai";
22
import { sourcebot_file_diff_review, sourcebot_file_diff_review_schema } from "@/features/agents/review-agent/types";
33
import { env } from "@sourcebot/shared";
4-
import fs from "fs";
5-
import path from "path";
4+
import { appendReviewAgentLog } from "@/features/agents/review-agent/lib"
65
import { createLogger } from "@sourcebot/shared";
76

87
const logger = createLogger('invoke-diff-review-llm');
98

10-
export const getReviewAgentLogDir = (): string => {
11-
return path.join(env.DATA_CACHE_DIR, 'review-agent');
12-
};
13-
14-
const validateLogPath = (logPath: string): void => {
15-
const resolved = path.resolve(logPath);
16-
const logDir = getReviewAgentLogDir();
17-
if (!resolved.startsWith(logDir + path.sep)) {
18-
throw new Error('reviewAgentLogPath escapes log directory');
19-
}
20-
};
21-
22-
export const invokeDiffReviewLlm = async (reviewAgentLogPath: string | undefined, prompt: string): Promise<sourcebot_file_diff_review> => {
9+
export const invokeDiffReviewLlm = async (reviewAgentLogFileName: string | undefined, prompt: string): Promise<sourcebot_file_diff_review> => {
2310
logger.debug("Executing invoke_diff_review_llm");
2411

2512
if (!env.OPENAI_API_KEY) {
@@ -31,9 +18,8 @@ export const invokeDiffReviewLlm = async (reviewAgentLogPath: string | undefined
3118
apiKey: env.OPENAI_API_KEY,
3219
});
3320

34-
if (reviewAgentLogPath) {
35-
validateLogPath(reviewAgentLogPath);
36-
fs.appendFileSync(reviewAgentLogPath, `\n\nPrompt:\n${prompt}`);
21+
if (reviewAgentLogFileName) {
22+
appendReviewAgentLog(reviewAgentLogFileName, `\n\nPrompt:\n${prompt}`);
3723
}
3824

3925
try {
@@ -45,9 +31,8 @@ export const invokeDiffReviewLlm = async (reviewAgentLogPath: string | undefined
4531
});
4632

4733
const openaiResponse = completion.choices[0].message.content;
48-
if (reviewAgentLogPath) {
49-
validateLogPath(reviewAgentLogPath);
50-
fs.appendFileSync(reviewAgentLogPath, `\n\nResponse:\n${openaiResponse}`);
34+
if (reviewAgentLogFileName) {
35+
appendReviewAgentLog(reviewAgentLogFileName, `\n\nResponse:\n${openaiResponse}`);
5136
}
5237

5338
const diffReviewJson = JSON.parse(openaiResponse || '{}');

0 commit comments

Comments
 (0)