Skip to content

Commit 6f236da

Browse files
authored
Merge pull request #122 from jeremymcs/codex/pr-qa-diff-context
feat: enrich PR question context
2 parents 3de5527 + d0496dc commit 6f236da

5 files changed

Lines changed: 169 additions & 6 deletions

File tree

‎docs/public/pr-questions.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,10 @@ When answering a question, the agent receives:
2828
- PR metadata such as title, number, repository, branch, author, URL, status, and last check time.
2929
- Current test/lint pass/fail fields when known.
3030
- Feedback items stored on the PR, including status, decision, author, file, line, and a body excerpt.
31+
- Changed files, bounded patch excerpts, and recent commit summaries fetched from GitHub when authentication is available.
3132
- The most recent 50 activity log entries for the PR.
3233

33-
The Q&A agent does not fetch the full diff, commit history, external docs, or issue trackers unless that information is already present in the stored PR context or logs.
34+
The Q&A agent does not fetch external documentation or issue trackers unless that information is already present in the stored PR context or logs. Very large diffs are bounded before they are sent to the agent.
3435

3536
## Use Cases
3637

‎server/backgroundJobHandlers.test.ts‎

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ test("answer_pr_question handler delegates for non-terminal questions", async ()
5858
`answer_pr_question:${question.id}`,
5959
{ prId },
6060
);
61-
const calls: Array<{ prId: string; questionId: string; question: string; preferredAgent: string }> = [];
61+
const calls: Array<{ prId: string; questionId: string; question: string; preferredAgent: string; changedFiles?: string }> = [];
6262

6363
const handlers = createBackgroundJobHandlers({
6464
storage,
@@ -68,8 +68,30 @@ test("answer_pr_question handler delegates for non-terminal questions", async ()
6868
questionId: params.questionId,
6969
question: params.question,
7070
preferredAgent: params.preferredAgent,
71+
changedFiles: params.repositoryContext?.changedFiles,
7172
});
7273
},
74+
deps: {
75+
resolveGitHubAuthTokenFn: async () => "token",
76+
buildOctokitFn: () => ({
77+
pulls: {
78+
listFiles: async () => ({
79+
data: [
80+
{ filename: "src/widget.ts", status: "modified", additions: 10, deletions: 2, changes: 12, patch: "@@ -1 +1 @@\n-old\n+new" },
81+
],
82+
}),
83+
listCommits: async () => ({
84+
data: [
85+
{
86+
sha: "abcdef123456",
87+
commit: { message: "feat: add widget\n\nbody", author: { name: "Alice" } },
88+
author: { login: "alice" },
89+
},
90+
],
91+
}),
92+
},
93+
}) as never,
94+
},
7395
});
7496

7597
await handlers.answer_pr_question!(job);
@@ -80,6 +102,7 @@ test("answer_pr_question handler delegates for non-terminal questions", async ()
80102
questionId: question.id,
81103
question: "What changed?",
82104
preferredAgent: "claude",
105+
changedFiles: "- src/widget.ts (modified, +10/-2, 12 changes)\n@@ -1 +1 @@\n-old\n+new",
83106
});
84107
});
85108

‎server/backgroundJobHandlers.ts‎

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import { buildIssueReplyBody, buildIssueVerifyComment, buildIssueWorkStatusComme
2222
import { decomposeIssueBody, hashIssueBody } from "./issueDecompose";
2323
import { verifySubtasksAgainstPr } from "./issueVerify";
2424
import { runIssueWorkRepair } from "./issueWorkAgent";
25-
import { answerPRQuestion } from "./prQuestionAgent";
25+
import { answerPRQuestion, type PRQuestionRepositoryContext } from "./prQuestionAgent";
2626
import { getRateLimitState } from "./rateLimitState";
2727
import type { ReleaseManager } from "./releaseManager";
2828
import { runWithRequestPriority } from "./requestPriority";
@@ -112,6 +112,60 @@ export function createBackgroundJobHandlers(params: {
112112
const resolveGitHubAuthTokenFn = params.deps?.resolveGitHubAuthTokenFn ?? resolveGitHubAuthToken;
113113
const runDeploymentHealingRepairFn = params.deps?.runDeploymentHealingRepairFn ?? runDeploymentHealingRepair;
114114

115+
async function buildQuestionRepositoryContext(prUrl: string): Promise<PRQuestionRepositoryContext | null> {
116+
const parsed = parsePRUrl(prUrl);
117+
if (!parsed) return null;
118+
119+
try {
120+
const octokit = await buildOctokitFn(await storage.getConfig());
121+
const [filesResponse, commitsResponse] = await Promise.all([
122+
octokit.pulls.listFiles({
123+
owner: parsed.owner,
124+
repo: parsed.repo,
125+
pull_number: parsed.number,
126+
per_page: 100,
127+
}),
128+
octokit.pulls.listCommits({
129+
owner: parsed.owner,
130+
repo: parsed.repo,
131+
pull_number: parsed.number,
132+
per_page: 30,
133+
}),
134+
]);
135+
136+
const changedFiles = filesResponse.data.map((file) => {
137+
const patch = typeof file.patch === "string" && file.patch.trim()
138+
? `\n${file.patch.split("\n").slice(0, 80).join("\n")}`
139+
: "";
140+
return [
141+
`- ${file.filename} (${file.status}, +${file.additions}/-${file.deletions}, ${file.changes} changes)`,
142+
patch,
143+
].join("");
144+
}).join("\n\n");
145+
146+
const commits = commitsResponse.data.map((commit) => {
147+
const message = commit.commit.message.split("\n")[0] ?? "";
148+
const author = commit.commit.author?.name ?? commit.author?.login ?? "unknown";
149+
return `- ${commit.sha.slice(0, 7)} ${message} (${author})`;
150+
}).join("\n");
151+
152+
return {
153+
changedFiles,
154+
commits,
155+
note: filesResponse.data.length >= 100
156+
? "Only the first 100 changed files were included."
157+
: undefined,
158+
};
159+
} catch (error) {
160+
const message = error instanceof Error ? error.message : String(error);
161+
return {
162+
changedFiles: "(unavailable)",
163+
commits: "(unavailable)",
164+
note: `Could not fetch live GitHub change context: ${message.slice(0, 300)}`,
165+
};
166+
}
167+
}
168+
115169
async function postIssueWorkStatusComment(
116170
octokit: {
117171
issues: {
@@ -661,6 +715,7 @@ export function createBackgroundJobHandlers(params: {
661715
question: question.question,
662716
preferredAgent: resolveRepoCodingAgent(config, repoSettings),
663717
agentSettings: resolveRepoAgentRuntimeSettings(config, repoSettings),
718+
repositoryContext: pr ? await buildQuestionRepositoryContext(pr.url) : null,
664719
});
665720
},
666721

‎server/prQuestionAgent.test.ts‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,35 @@ async function withFakeAgent(
5858
}
5959
}
6060

61+
async function withCapturingFakeAgent(
62+
run: (promptPath: string) => Promise<void>,
63+
): Promise<void> {
64+
const tempRoot = await mkdtemp(path.join(os.tmpdir(), "fake-agent-bin-"));
65+
const promptPath = path.join(tempRoot, "prompt.txt");
66+
const fakeClaudePath = path.join(tempRoot, "claude");
67+
const originalPath = process.env.PATH;
68+
69+
try {
70+
await writeFile(
71+
fakeClaudePath,
72+
[
73+
"#!/usr/bin/env node",
74+
"const fs = require('node:fs');",
75+
`fs.writeFileSync(${JSON.stringify(promptPath)}, process.argv[process.argv.length - 1] || '', 'utf8');`,
76+
"process.stdout.write('Answer from captured context');",
77+
"",
78+
].join("\n"),
79+
"utf8",
80+
);
81+
await chmod(fakeClaudePath, 0o755);
82+
process.env.PATH = [tempRoot, originalPath].filter(Boolean).join(path.delimiter);
83+
await run(promptPath);
84+
} finally {
85+
process.env.PATH = originalPath;
86+
await rm(tempRoot, { recursive: true, force: true });
87+
}
88+
}
89+
6190
describe("answerPRQuestion", () => {
6291
it("sets status to 'error' when the PR does not exist", async () => {
6392
const storage = new MemStorage();
@@ -272,4 +301,29 @@ describe("answerPRQuestion", () => {
272301
assert.equal(updated.answer, null);
273302
});
274303
});
304+
305+
it("includes bounded repository change context in the agent prompt", async () => {
306+
await withCapturingFakeAgent(async (promptPath) => {
307+
const storage = new MemStorage();
308+
const prId = await seedPR(storage);
309+
const q = await storage.addQuestion(prId, "What changed?");
310+
311+
await answerPRQuestion({
312+
storage,
313+
prId,
314+
questionId: q.id,
315+
question: q.question,
316+
preferredAgent: "claude",
317+
repositoryContext: {
318+
changedFiles: "- src/widget.ts (modified, +10/-2, 12 changes)\n@@ -1 +1 @@\n-old\n+new",
319+
commits: "- abcdef1 feat: add widget (Alice)",
320+
},
321+
});
322+
323+
const prompt = await import("node:fs/promises").then((fs) => fs.readFile(promptPath, "utf8"));
324+
assert.match(prompt, /## Repository Change Context/);
325+
assert.match(prompt, /src\/widget\.ts/);
326+
assert.match(prompt, /abcdef1 feat: add widget/);
327+
});
328+
});
275329
});

‎server/prQuestionAgent.ts‎

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,14 @@ export async function answerPRQuestion(params: {
1313
question: string;
1414
preferredAgent: CodingAgent;
1515
agentSettings?: AgentRuntimeSettings;
16+
repositoryContext?: PRQuestionRepositoryContext | null;
1617
}): Promise<void> {
17-
const { storage, prId, questionId, question, preferredAgent, agentSettings } = params;
18+
const { storage, prId, questionId, question, preferredAgent, agentSettings, repositoryContext } = params;
1819

1920
await storage.updateQuestion(questionId, { status: "answering" });
2021

2122
try {
22-
const context = await buildPRContext(storage, prId);
23+
const context = await buildPRContext(storage, prId, repositoryContext ?? null);
2324
const agent = await resolveAgent(preferredAgent);
2425
const prompt = buildPrompt(context, question);
2526

@@ -77,9 +78,25 @@ type PRContext = {
7778
lastChecked: string | null;
7879
feedbackSummary: string;
7980
recentLogs: string;
81+
repositoryContext: string;
8082
};
8183

82-
async function buildPRContext(storage: IStorage, prId: string): Promise<PRContext> {
84+
export type PRQuestionRepositoryContext = {
85+
changedFiles: string;
86+
commits: string;
87+
note?: string;
88+
};
89+
90+
function truncateForPrompt(value: string, maxLength: number) {
91+
if (value.length <= maxLength) return value;
92+
return `${value.slice(0, maxLength - 25).trimEnd()}\n[truncated for context]`;
93+
}
94+
95+
async function buildPRContext(
96+
storage: IStorage,
97+
prId: string,
98+
repositoryContext: PRQuestionRepositoryContext | null,
99+
): Promise<PRContext> {
83100
const pr = await storage.getPR(prId);
84101
if (!pr) throw new Error("PR not found");
85102

@@ -113,6 +130,16 @@ async function buildPRContext(storage: IStorage, prId: string): Promise<PRContex
113130
lastChecked: pr.lastChecked,
114131
feedbackSummary: feedbackLines.length > 0 ? feedbackLines.join("\n") : "(no feedback items)",
115132
recentLogs: recentLogs || "(no recent activity)",
133+
repositoryContext: repositoryContext
134+
? [
135+
repositoryContext.note ? `Note: ${repositoryContext.note}` : "",
136+
"Changed files and patch excerpts:",
137+
truncateForPrompt(repositoryContext.changedFiles || "(no changed files available)", 12000),
138+
"",
139+
"Recent commits:",
140+
truncateForPrompt(repositoryContext.commits || "(no commit context available)", 4000),
141+
].filter(Boolean).join("\n")
142+
: "(repository diff and commit context unavailable)",
116143
};
117144
}
118145

@@ -136,6 +163,9 @@ function buildPrompt(ctx: PRContext, question: string): string {
136163
"## Feedback Items",
137164
ctx.feedbackSummary,
138165
"",
166+
"## Repository Change Context",
167+
ctx.repositoryContext,
168+
"",
139169
"## Recent Activity Logs (most recent 50 entries)",
140170
ctx.recentLogs,
141171
"",

0 commit comments

Comments
 (0)