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
63 changes: 43 additions & 20 deletions packages/core/src/evaluation/results-repo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -985,6 +985,39 @@ export async function createDraftResultsPr(params: {

const DIRECT_PUSH_MAX_RETRIES = 3;

async function hasUnpushedCommits(repoDir: string, baseBranch: string): Promise<boolean> {
const { stdout } = await runGit(['rev-list', '--count', `origin/${baseBranch}..HEAD`], {
cwd: repoDir,
check: false,
});
return Number.parseInt(stdout.trim(), 10) > 0;
}

async function pushDirectResultsToBase(params: {
readonly normalized: Required<ResultsConfig>;
readonly repoDir: string;
readonly baseBranch: string;
}): Promise<void> {
for (let attempt = 1; attempt <= DIRECT_PUSH_MAX_RETRIES; attempt++) {
try {
await runGit(['push', 'origin', `HEAD:${params.baseBranch}`], { cwd: params.repoDir });
updateStatusFile(params.normalized, {
last_synced_at: new Date().toISOString(),
last_error: undefined,
});
return;
} catch (error) {
const message = error instanceof Error ? error.message : String(error);
if (attempt < DIRECT_PUSH_MAX_RETRIES && message.includes('non-fast-forward')) {
await fetchResultsRepo(params.repoDir);
await runGit(['rebase', `origin/${params.baseBranch}`], { cwd: params.repoDir });
} else {
throw error;
}
}
}
}

/**
* Push results directly to the base branch of the results repo.
* Handles non-fast-forward conflicts by fetching, rebasing, and retrying.
Expand Down Expand Up @@ -1020,33 +1053,23 @@ export async function directPushResults(params: {
check: false,
});
if (status.trim().length === 0) {
if (await hasUnpushedCommits(repoDir, baseBranch)) {
const aheadPaths = await getAheadPaths(repoDir, `origin/${baseBranch}`);
if (!areSafeResultsRepoPaths(aheadPaths)) {
throw new Error('Results repo has non-results committed changes');
}
await pushDirectResultsToBase({ normalized, repoDir, baseBranch });
return true;
}
return false;
}

await runGit(['commit', '-m', params.commitMessage, '-m', `Agentv-Run: ${targetRunId}`], {
cwd: repoDir,
});

for (let attempt = 1; attempt <= DIRECT_PUSH_MAX_RETRIES; attempt++) {
try {
await runGit(['push', 'origin', `HEAD:${baseBranch}`], { cwd: repoDir });
updateStatusFile(normalized, {
last_synced_at: new Date().toISOString(),
last_error: undefined,
});
return true;
} catch (error) {
const message = error instanceof Error ? error.message : String(error);
if (attempt < DIRECT_PUSH_MAX_RETRIES && message.includes('non-fast-forward')) {
await fetchResultsRepo(repoDir);
await runGit(['rebase', `origin/${baseBranch}`], { cwd: repoDir });
} else {
throw error;
}
}
}

return false;
await pushDirectResultsToBase({ normalized, repoDir, baseBranch });
return true;
}

export interface GitListedRun {
Expand Down
52 changes: 51 additions & 1 deletion packages/core/test/evaluation/results-repo.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { execSync } from 'node:child_process';
import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
import { chmodSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
import os from 'node:os';
import path from 'node:path';

Expand Down Expand Up @@ -308,6 +308,56 @@ describe('results repo write path', () => {
rmSync(rootDir, { recursive: true, force: true });
});

it('retries an interrupted direct push without dropping the committed run', async () => {
const { remoteDir } = initializeRemoteRepo(rootDir);
const cloneDir = path.join(rootDir, 'results-clone');
const sourceDir = path.join(rootDir, 'source-run');
const runTimestamp = '2026-05-22T11-00-00-000Z';
const destinationPath = path.join('retry', runTimestamp);
const config = createResultsConfig(remoteDir, cloneDir);
const hookPath = path.join(remoteDir, 'hooks', 'pre-receive');
writeRunArtifacts(sourceDir, 'retry', '2026-05-22T11:00:00.000Z');

await ensureResultsRepoClone(config);
git('git config user.email "test@example.com"', cloneDir);
git('git config user.name "Test User"', cloneDir);

writeFileSync(hookPath, '#!/usr/bin/env sh\necho "simulated interrupted push" >&2\nexit 1\n');
chmodSync(hookPath, 0o755);

await expect(
directPushResults({
config,
sourceDir,
destinationPath,
commitMessage: 'feat(results): retry - 1/1 PASS (1.000)',
}),
).rejects.toThrow(/simulated interrupted push/);
expect(git('git rev-list --count origin/main..HEAD', cloneDir)).toBe('1');
expect(git(`git --git-dir "${remoteDir}" ls-tree -r --name-only main`, rootDir)).not.toContain(
`.agentv/results/runs/retry/${runTimestamp}/benchmark.json`,
);

rmSync(hookPath, { force: true });

await expect(
directPushResults({
config,
sourceDir,
destinationPath,
commitMessage: 'feat(results): retry - 1/1 PASS (1.000)',
}),
).resolves.toBe(true);

expect(git('git rev-list --count origin/main..HEAD', cloneDir)).toBe('0');
expect(git(`git --git-dir "${remoteDir}" ls-tree -r --name-only main`, rootDir)).toContain(
`.agentv/results/runs/retry/${runTimestamp}/benchmark.json`,
);
expect(git(`git --git-dir "${remoteDir}" log -1 --pretty=%B main`, rootDir)).toContain(
`Agentv-Run: retry::${runTimestamp}`,
);
}, 20000);

it('commits pushed runs into the configured clone with an Agentv-Run trailer', async () => {
const { remoteDir } = initializeRemoteRepo(rootDir);
const cloneDir = path.join(rootDir, 'results-clone');
Expand Down
Loading