Skip to content

armgit / eve

Public Imported from GitHub
0

The Open Framework for Building Agents

Fix commit SHA inconsistency between context and dispatch (#123)

Caleb An2057b438ec21
diff --git a/packages/eve/src/public/channels/github/api.ts b/packages/eve/src/public/channels/github/api.ts
index dadf8b9ef..fab9595cb 100644
--- a/packages/eve/src/public/channels/github/api.ts
+++ b/packages/eve/src/public/channels/github/api.ts
@@ -354,6 +354,23 @@ export async function listGitHubPullRequestFiles(
return response.body.map(toPullRequestFile);
}
+/** Lists files changed between two commits (three-dot compare). */
+export async function listGitHubCompareFiles(
+ input: GitHubResourceInput & {
+ readonly baseSha: string;
+ readonly headSha: string;
+ },
+): Promise<readonly GitHubPullRequestFile[]> {
+ const response = await callGitHubApi<{ readonly files?: unknown[] }>({
+ api: input.api,
+ credentials: input.credentials,
+ installationId: input.installationId,
+ method: "GET",
+ path: `/repos/${encodePath(input.owner)}/${encodePath(input.repo)}/compare/${input.baseSha}...${input.headSha}`,
+ });
+ return (response.body.files ?? []).map(toPullRequestFile);
+}
+
/** Creates a reaction on a GitHub issue or review comment. */
export function createGitHubReaction(
input: GitHubResourceInput & {
diff --git a/packages/eve/src/public/channels/github/dispatch.ts b/packages/eve/src/public/channels/github/dispatch.ts
index 3eeeb8899..f450f75c9 100644
--- a/packages/eve/src/public/channels/github/dispatch.ts
+++ b/packages/eve/src/public/channels/github/dispatch.ts
@@ -256,8 +256,10 @@ async function buildPullRequestContext(
try {
return await buildGitHubPullRequestContext({
api: config.api,
+ baseSha: state.baseSha,
config: config.pullRequestContext,
credentials: config.credentials,
+ headSha: state.headSha,
installationId: state.installationId ?? undefined,
owner: state.owner,
pullRequestNumber: state.pullRequestNumber,
diff --git a/packages/eve/src/public/channels/github/pr-context.test.ts b/packages/eve/src/public/channels/github/pr-context.test.ts
index e6919d2fa..82c8988f9 100644
--- a/packages/eve/src/public/channels/github/pr-context.test.ts
+++ b/packages/eve/src/public/channels/github/pr-context.test.ts
@@ -68,6 +68,87 @@ function build(fetchMock: typeof fetch, config?: { excludedFiles?: readonly stri
});
}
+/**
+ * SHA pinning for webhook-driven PR turns.
+ *
+ * Dispatch copies `state.headSha` and `state.baseSha` from the webhook into
+ * {@link buildGitHubPullRequestContext} so injected diffs and sandbox checkout
+ * refer to the same commit.
+ *
+ * Timing gap this covers (single delivery):
+ * T0 Webhook arrives for commit ABC → state.headSha = ABC
+ * T1 Contributor pushes commit XYZ before context is built
+ * T2 GET /pulls/N returns live head XYZ
+ * Without pinning: /pulls/N/files would load XYZ's diff while checkout still uses ABC.
+ * With pinning: compare base...ABC loads ABC's diff; metadata shows head_sha: ABC.
+ *
+ * Out of scope for these unit tests:
+ * - Two overlapping synchronize deliveries (separate webhooks)
+ * - Head moving after context is built but before the turn completes
+ * - Title/body from GET /pulls/N while SHAs stay pinned
+ */
+describe("SHA pinning", () => {
+ it("keeps webhook ABC when live PR head is XYZ at context build time", async () => {
+ const webhookHead = "abc111fromwebhook";
+ const webhookBase = "base999fromwebhook";
+ const liveHead = "xyz999alreadyongithub";
+ const liveFilesPatch = "@@ patch for the current PR head";
+
+ const fetchMock = vi.fn((input: Request | URL | string) => {
+ const url = String(input);
+ if (url.includes("/files")) {
+ return Promise.reject(new Error("did not expect /pulls/N/files when headSha is pinned"));
+ }
+ if (url.includes("/compare/")) {
+ return Promise.resolve(
+ jsonResponse({
+ files: [
+ {
+ additions: 1,
+ deletions: 0,
+ filename: "packages/eve/src/at-abc.ts",
+ patch: "@@ diff for abc111",
+ status: "added",
+ },
+ ],
+ }),
+ );
+ }
+ return Promise.resolve(
+ jsonResponse(
+ pullRequest({
+ base: { ref: "main", repo: { full_name: "vercel/eve" }, sha: "other-live-base" },
+ head: { ref: "feature/github", repo: { full_name: "octocat/eve" }, sha: liveHead },
+ }),
+ ),
+ );
+ });
+
+ const messages = await buildGitHubPullRequestContext({
+ api: { apiBaseUrl: "https://github.test", fetch: fetchMock },
+ baseSha: webhookBase,
+ credentials,
+ headSha: webhookHead,
+ installationId: 55,
+ owner: "vercel",
+ pullRequestNumber: 7,
+ repo: "eve",
+ });
+
+ expect(fetchMock.mock.calls[0]?.[0]).toBe("https://github.test/repos/vercel/eve/pulls/7");
+ expect(fetchMock.mock.calls[1]?.[0]).toBe(
+ `https://github.test/repos/vercel/eve/compare/${webhookBase}...${webhookHead}`,
+ );
+ expect(fetchMock).toHaveBeenCalledTimes(2);
+ expect(text(messages)).toContain(`head_sha: ${webhookHead}`);
+ expect(text(messages)).toContain(`base_sha: ${webhookBase}`);
+ expect(text(messages)).not.toContain(liveHead);
+ expect(text(messages)).toContain("- packages/eve/src/at-abc.ts");
+ expect(text(messages)).toContain("@@ diff for abc111");
+ expect(text(messages)).not.toContain(liveFilesPatch);
+ });
+});
+
describe("GitHub pull-request context", () => {
it("builds metadata and the changed-file diff by default", async () => {
const fetchMock = vi
diff --git a/packages/eve/src/public/channels/github/pr-context.ts b/packages/eve/src/public/channels/github/pr-context.ts
index dd16736bd..9fa43a99a 100644
--- a/packages/eve/src/public/channels/github/pr-context.ts
+++ b/packages/eve/src/public/channels/github/pr-context.ts
@@ -1,5 +1,6 @@
import {
getGitHubPullRequest,
+ listGitHubCompareFiles,
listGitHubPullRequestFiles,
type GitHubApiOptions,
type GitHubPullRequestDetails,
@@ -52,8 +53,18 @@ export interface GitHubPullRequestContextConfig {
/** Input for building one-shot model context for a pull request. */
export interface GitHubPullRequestContextInput {
readonly api?: GitHubApiOptions;
+ /**
+ * When set with {@link headSha}, file patches load via the compare API for
+ * this exact base/head pair instead of `/pulls/{number}/files` (current head).
+ */
+ readonly baseSha?: string | null;
readonly config?: GitHubPullRequestContextConfig;
readonly credentials?: GitHubChannelCredentials;
+ /**
+ * Webhook-resolved head SHA for this turn. When set, metadata and diffs pin to
+ * this commit so injected context matches sandbox checkout.
+ */
+ readonly headSha?: string | null;
readonly installationId?: number;
readonly owner: string;
readonly pullRequestNumber: number | null;
@@ -72,25 +83,45 @@ export async function buildGitHubPullRequestContext(
): Promise<readonly string[] | undefined> {
if (input.pullRequestNumber === null) return undefined;
- const details = await getGitHubPullRequest({
- api: input.api,
- credentials: input.credentials,
- installationId: input.installationId,
- owner: input.owner,
- pullRequestNumber: input.pullRequestNumber,
- repo: input.repo,
- });
+ const details = withPinnedPullRequestShas(
+ await getGitHubPullRequest({
+ api: input.api,
+ credentials: input.credentials,
+ installationId: input.installationId,
+ owner: input.owner,
+ pullRequestNumber: input.pullRequestNumber,
+ repo: input.repo,
+ }),
+ input,
+ );
const lines = renderPullRequestMetadata(details);
- const files = await listGitHubPullRequestFiles({
- api: input.api,
- credentials: input.credentials,
- installationId: input.installationId,
- owner: input.owner,
- perPage: MAX_FILES,
- pullRequestNumber: input.pullRequestNumber,
- repo: input.repo,
- });
+ const pinnedBaseSha = readNonEmptySha(input.baseSha) ?? readNonEmptySha(details.base.sha);
+ const pinnedHeadSha = readNonEmptySha(input.headSha) ?? readNonEmptySha(details.head.sha);
+ const files =
+ readNonEmptySha(input.headSha) !== undefined &&
+ pinnedHeadSha !== undefined &&
+ pinnedBaseSha !== undefined
+ ? (
+ await listGitHubCompareFiles({
+ api: input.api,
+ baseSha: pinnedBaseSha,
+ credentials: input.credentials,
+ headSha: pinnedHeadSha,
+ installationId: input.installationId,
+ owner: input.owner,
+ repo: input.repo,
+ })
+ ).slice(0, MAX_FILES)
+ : await listGitHubPullRequestFiles({
+ api: input.api,
+ credentials: input.credentials,
+ installationId: input.installationId,
+ owner: input.owner,
+ perPage: MAX_FILES,
+ pullRequestNumber: input.pullRequestNumber,
+ repo: input.repo,
+ });
const excluded = [...GITHUB_DEFAULT_EXCLUDED_DIFF_FILES, ...(input.config?.excludedFiles ?? [])];
lines.push("", ...renderPullRequestFiles(files, excluded));
@@ -115,6 +146,24 @@ export function mergeGitHubContext(input: {
return [...github, ...hook];
}
+function withPinnedPullRequestShas(
+ details: GitHubPullRequestDetails,
+ input: Pick<GitHubPullRequestContextInput, "baseSha" | "headSha">,
+): GitHubPullRequestDetails {
+ const headSha = readNonEmptySha(input.headSha);
+ const baseSha = readNonEmptySha(input.baseSha);
+ if (headSha === undefined && baseSha === undefined) return details;
+ return {
+ ...details,
+ base: { ...details.base, sha: baseSha ?? details.base.sha },
+ head: { ...details.head, sha: headSha ?? details.head.sha },
+ };
+}
+
+function readNonEmptySha(value: string | null | undefined): string | undefined {
+ return typeof value === "string" && value.trim().length > 0 ? value.trim() : undefined;
+}
+
function renderPullRequestMetadata(details: GitHubPullRequestDetails): string[] {
return [
"<github_pull_request>",