Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/agent-evals-fork-point.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@rnx-kit/agent-evals": patch
---

Local sessions no longer audit commits that landed on the base branch during
the session, e.g. after rebasing onto it. The base branch defaults to the
default branch of `remote` and can be set with the new `baseBranch` option.
10 changes: 10 additions & 0 deletions .changeset/tools-git-cwd-remote.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
"@rnx-kit/tools-git": patch
---

`getDefaultBranch()` and `getBaseCommit()` now take options for the repository
directory (`cwd`) and remote (`remote`), and `getBaseCommit()` also takes the
commit to look up (`head`). `getDefaultBranch()` now returns the branch that
`<remote>/HEAD` points to, if it exists, even on CI. On CI, the fallback now
defaults to `<remote>/main`. Elsewhere, if `<remote>/HEAD` doesn't exist, it
asks the remote for its default branch instead of throwing.
8 changes: 5 additions & 3 deletions incubator/agent-evals/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,10 @@ it changed, and its requests and replies. What it audits depends on the vars:
(default: `~/.copilot`). If empty, the session running the evals, or if there
is none, the most recently updated session in the repository that is not in
use; `previous` always picks the latter. The changes are those on the
session's branch since it started. The session's branch is the one it last
switched to, according to the reflog of `HEAD`, or else the one it started
on, under the name it was last renamed to.
session's branch since it started, or since it forked off `baseBranch` if
that is later, e.g. if the branch was rebased during the session. The
session's branch is the one it last switched to, according to the reflog of
`HEAD`, or else the one it started on, under the name it was last renamed to.
- `pullRequest`: number of a pull request. Its Copilot cloud agent sessions are
read from GitHub, and the changes are those since it branched off its base
branch. If the sessions cannot be read, e.g. if the pull request was made in
Expand All @@ -114,6 +115,7 @@ The provider accepts the following `config`:
| Option | Description |
| :-------------- | :--------------------------------------------------------------------------------- |
| `remote` | Remote to look up branches and the repository on; defaults to `origin` |
| `baseBranch` | Branch that pull requests merge into; defaults to the default branch of `remote` |
| `repository` | Repository, e.g. `owner/repo` on GitHub; defaults to the repository of `remote` |
| `copilotApiUrl` | Copilot API to read cloud agent sessions from; defaults to `api.githubcopilot.com` |

Expand Down
1 change: 1 addition & 0 deletions incubator/agent-evals/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"dependencies": {
"@changesets/parse": "^1.0.0",
"@rnx-kit/tools-filesystem": "workspace:^",
"@rnx-kit/tools-git": "workspace:^",
"@rnx-kit/tools-shell": "workspace:^",
"parse-diff": "catalog:",
"shell-quote": "^1.8.4"
Expand Down
18 changes: 9 additions & 9 deletions incubator/agent-evals/src/provider/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@ const GH_READS: Record<string, string[]> = {
pr: ["checkout", "checks", "co", "diff", "list", "ls", "status", "view"],
};

const UNKNOWN = "unknown";

/**
* A write to GitHub: the number of the issue or pull request written to,
* `UNKNOWN` if it could not be determined, or `undefined` if not a write to an
* existing item in the repository.
*/
type Write = number | typeof UNKNOWN | undefined;

/**
* Returns the number in `123`, `#123` or an issue or pull request URL.
*/
Expand All @@ -41,15 +50,6 @@ function isRepository(repository: string, repo: unknown): boolean {
);
}

const UNKNOWN = "unknown";

/**
* A write to GitHub: the number of the issue or pull request written to,
* `UNKNOWN` if it could not be determined, or `undefined` if not a write to an
* existing item in the repository.
*/
type Write = number | typeof UNKNOWN | undefined;

/** GitHub MCP tools that write to a comment identified only by its ID */
const MCP_COMMENT_TOOLS = [
"add_reply_to_pull_request_comment",
Expand Down
10 changes: 5 additions & 5 deletions incubator/agent-evals/src/provider/grader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ const RUNTIME_ENV = [
"SSL_CERT_FILE",
];

type ChatMessage = {
role: string;
content: string;
};

/**
* Returns the environment for the Copilot runtime. The home directory is
* replaced with `home`, so that user config and memories are not used.
Expand Down Expand Up @@ -101,11 +106,6 @@ export function getToken(): string {
return cachedToken;
}

type ChatMessage = {
role: string;
content: string;
};

function isChat(value: unknown): value is ChatMessage[] {
return (
Array.isArray(value) &&
Expand Down
54 changes: 51 additions & 3 deletions incubator/agent-evals/src/provider/provider.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { getBaseCommit, getDefaultBranch } from "@rnx-kit/tools-git";
import type { AgentRun, EvalVars } from "../types.ts";
import { addWorktree, collectChanges, git } from "./checkout.ts";
import { github } from "./github.ts";
Expand All @@ -14,6 +15,11 @@ type ProviderConfig = {
repository?: string;
/** Remote to look up branches and the repository on; defaults to `origin` */
remote?: string;
/**
* Branch that pull requests merge into; defaults to the default branch of
* `remote`. Changes already on it are not audited.
*/
baseBranch?: string;
/** Copilot API to read cloud agent sessions from */
copilotApiUrl?: string;
};
Expand Down Expand Up @@ -86,6 +92,42 @@ async function resolveBranch(
return "";
}

/**
* Returns the commit to compare a session's changes with. That is where
* `head`, on the session's `branch`, forked off the base branch, if the
* session's start commit (`baseCommit`) precedes it, e.g. if the branch was
* rebased onto, or recreated from, the base branch during the session.
* Otherwise, or if the session worked on the base branch, it is `baseCommit`.
* Commits that already landed on the base branch are thus not audited.
*/
export async function forkPoint(
root: string,
{ remote = "origin", baseBranch }: ProviderConfig,
branch: string,
baseCommit: string,
head: string
): Promise<string> {
try {
const options = { cwd: root, remote, head };
const base = baseBranch
? `${remote}/${baseBranch}`
: getDefaultBranch(undefined, options);
if (base === `${remote}/${branch}`) {
return baseCommit;
}

const mergeBase = getBaseCommit(base, undefined, options);
if (!mergeBase) {
return baseCommit;
}

await git(root, ["merge-base", "--is-ancestor", baseCommit, mergeBase]);
return mergeBase;
} catch (_) {
return baseCommit;
}
}

/**
* Returns the branch the session ended on, given the branch it started on,
* when it ran (`startTime` to `endTime`, in milliseconds since the epoch), and
Expand Down Expand Up @@ -298,19 +340,25 @@ async function replay(
}

const workingTree = endCommit === "WORKING_TREE";
const head = workingTree ? "HEAD" : endCommit;
try {
const head = workingTree ? "HEAD" : endCommit;
await git(root, ["merge-base", "--is-ancestor", baseCommit, head]);
} catch (_) {
throw new Error(
`Branch '${branch}' does not contain start commit ${baseCommit} of session ${session.id}`
);
}

const forkCommit = await forkPoint(root, config, branch, baseCommit, head);
const workdir = workingTree ? root : await addWorktree(root, endCommit);
return {
output: await collectRun(host, repository, session, workdir, baseCommit),
metadata: { copilotSession: session.id, branch, baseCommit, endCommit },
output: await collectRun(host, repository, session, workdir, forkCommit),
metadata: {
copilotSession: session.id,
branch,
baseCommit: forkCommit,
endCommit,
},
};
}

Expand Down
14 changes: 7 additions & 7 deletions incubator/agent-evals/src/provider/report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ export type EvalResult = {
} | null;
};

/**
* Outcome of a test: `unchecked` if it could not be evaluated, e.g. because
* the grader is unavailable, and `skipped` if it needs the session log, which
* could not be read.
*/
export type Status = "passed" | "failed" | "unchecked" | "skipped";

function indent(text: string): string {
return text
.trim()
Expand Down Expand Up @@ -66,13 +73,6 @@ function isGraderError(result: EvalResult): boolean {
);
}

/**
* Outcome of a test: `unchecked` if it could not be evaluated, e.g. because
* the grader is unavailable, and `skipped` if it needs the session log, which
* could not be read.
*/
export type Status = "passed" | "failed" | "unchecked" | "skipped";

export function statusOf(result: EvalResult): Status {
if (result.success) {
return "passed";
Expand Down
90 changes: 89 additions & 1 deletion incubator/agent-evals/test/provider.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import { equal, rejects } from "node:assert/strict";
import { describe, it } from "node:test";
import * as fs from "node:fs";
import * as os from "node:os";
import * as path from "node:path";
import { after, before, describe, it } from "node:test";
import { fileURLToPath } from "node:url";
import { git } from "../src/provider/checkout.ts";
import { github, parseRepository } from "../src/provider/github.ts";
import CopilotSessionProvider, {
endBranch,
forkPoint,
resolveRepository,
} from "../src/provider/provider.ts";

Expand Down Expand Up @@ -91,6 +96,89 @@ describe("endBranch()", () => {
});
});

describe("forkPoint()", () => {
// `forkPoint()` runs the real `git`, so it needs a real repository
const root = fs.mkdtempSync(path.join(os.tmpdir(), "agent-evals-test-"));
const commits: Record<string, string> = {};

async function commit(name: string): Promise<void> {
await git(root, [
"-c",
"user.name=test",
"-c",
"user.email=test@example.com",
"commit",
"--quiet",
"--allow-empty",
"--no-verify",
"--no-gpg-sign",
"-m",
name,
]);
commits[name] = (await git(root, ["rev-parse", "HEAD"])).trim();
}

// main: A - B
// \ \
// old: C \
// feature: D
before(async () => {
await git(root, ["init", "--quiet", "--initial-branch=main"]);
await commit("A");
await git(root, ["switch", "--quiet", "-c", "old"]);
await commit("C");
await git(root, ["switch", "--quiet", "main"]);
await commit("B");
await git(root, ["switch", "--quiet", "-c", "feature"]);
await commit("D");
await git(root, ["switch", "--quiet", "main"]);
await git(root, ["update-ref", "refs/remotes/origin/main", commits["B"]]);
});

after(() => {
fs.rmSync(root, { recursive: true, force: true, maxRetries: 3 });
});

it("skips commits that landed on the base branch during the session", async () => {
const config = { baseBranch: "main" };
const { A, B, D } = commits;
equal(await forkPoint(root, config, "feature", A, D), B);
});

it("returns the start commit if it follows the fork point", async () => {
const config = { baseBranch: "main" };
const { A, C, D } = commits;
equal(await forkPoint(root, config, "old", C, C), C);
equal(await forkPoint(root, config, "feature", D, D), D);
equal(await forkPoint(root, config, "old", A, C), A);
});

it("returns the start commit on the base branch", async () => {
const { A, B } = commits;
equal(await forkPoint(root, { baseBranch: "main" }, "main", A, B), A);
});

it("returns the start commit if the base branch is not found", async () => {
const { A, D } = commits;
equal(await forkPoint(root, { baseBranch: "trunk" }, "feature", A, D), A);
equal(await forkPoint(root, { remote: "none" }, "feature", A, D), A);
});

it("defaults to the default branch of the remote", async () => {
const { A, B, D } = commits;
await git(root, ["remote", "add", "upstream", root]);
await git(root, ["update-ref", "refs/remotes/upstream/main", B]);
equal(await forkPoint(root, { remote: "upstream" }, "feature", A, D), B);

await git(root, [
"symbolic-ref",
"refs/remotes/origin/HEAD",
"refs/remotes/origin/main",
]);
equal(await forkPoint(root, {}, "feature", A, D), B);
});
});

describe("CopilotSessionProvider.callApi()", () => {
it("rejects invalid pull request numbers", async () => {
const provider = new CopilotSessionProvider();
Expand Down
14 changes: 7 additions & 7 deletions incubator/tools-git/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ if (baseCommit) {
<!-- The following table can be updated by running `yarn update-readme` -->
<!-- @rnx-kit/api start -->

| Category | Function | Description |
| -------- | --------------------------------------- | ----------------------------------------------------------- |
| branch | `getBaseCommit(targetBranch, fallback)` | Returns the commit that the current branch forked off. |
| branch | `getDefaultBranch(fallback)` | Returns the default branch for the current repository. |
| diff | `getChangedFiles(since)` | Returns all files that changed since a given branch/commit. |
| git | `git(...args)` | Executes `git` with specified arguments. |
| ref | `verifyRef(ref)` | Returns whether the specified reference name is valid. |
| Category | Function | Description |
| -------- | ------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| branch | `getBaseCommit(targetBranch, fallback, options)` | Returns the commit that the current branch forked off. |
| branch | `getDefaultBranch(fallback, options)` | Returns the default branch for the current repository, e.g. `origin/main`. That is the branch `<remote>/HEAD` points to, or else the one the remote reports. |
| diff | `getChangedFiles(since)` | Returns all files that changed since a given branch/commit. |
| git | `git(...args)` | Executes `git` with specified arguments. |
| ref | `verifyRef(ref)` | Returns whether the specified reference name is valid. |

<!-- @rnx-kit/api end -->
Loading
Loading