From b0eff07d222d52b061f20ed88155e5bec2afaf33 Mon Sep 17 00:00:00 2001 From: Brace Sproul Date: Thu, 29 May 2025 17:52:32 -0700 Subject: [PATCH] feat: If session id exists, pull latest changes on branch (#57) --- apps/open-swe/src/nodes/initialize.ts | 18 +++++++++--------- apps/open-swe/src/utils/git/index.ts | 24 ++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 9 deletions(-) diff --git a/apps/open-swe/src/nodes/initialize.ts b/apps/open-swe/src/nodes/initialize.ts index ab8f36da..8ecc3bce 100644 --- a/apps/open-swe/src/nodes/initialize.ts +++ b/apps/open-swe/src/nodes/initialize.ts @@ -12,6 +12,7 @@ import { configureGitUserInRepo, getBranchName, getRepoAbsolutePath, + pullLatestChanges, } from "../utils/git/index.js"; import { getSandboxErrorFields } from "../utils/sandbox-error-fields.js"; @@ -63,6 +64,13 @@ export async function initialize( throw new Error("Configuration object not found."); } const { sandboxSessionId } = state; + const { targetRepository } = state; + if (!targetRepository) { + throw new Error( + "Missing required target repository. Please provide a git repository in state or configuration.", + ); + } + const absoluteRepoDir = getRepoAbsolutePath(targetRepository); if (sandboxSessionId) { try { @@ -74,6 +82,7 @@ export async function initialize( sandboxSessionId, TIMEOUT_EXTENSION_OPT, ); + await pullLatestChanges(absoluteRepoDir, newSandbox); return { sandboxSessionId: newSandbox.sandboxId, }; @@ -83,13 +92,6 @@ export async function initialize( } } - const { targetRepository } = state; - if (!targetRepository) { - throw new Error( - "Missing required target repository. Please provide a git repository in state or configuration.", - ); - } - logger.info("Creating sandbox..."); const sandbox = await Sandbox.create( SANDBOX_TEMPLATE_ID, @@ -104,8 +106,6 @@ export async function initialize( } logger.info("Repository cloned successfully."); - const absoluteRepoDir = getRepoAbsolutePath(targetRepository); - logger.info(`Configuring git user for repository at "${absoluteRepoDir}"...`); await configureGitUserInRepo(absoluteRepoDir, sandbox); logger.info("Git user configured successfully."); diff --git a/apps/open-swe/src/utils/git/index.ts b/apps/open-swe/src/utils/git/index.ts index eb6e8767..3297f12b 100644 --- a/apps/open-swe/src/utils/git/index.ts +++ b/apps/open-swe/src/utils/git/index.ts @@ -449,3 +449,27 @@ export async function createPullRequest({ return null; } } + +export async function pullLatestChanges( + absoluteRepoDir: string, + sandbox: Sandbox, +): Promise { + try { + const gitPullOutput = await sandbox.commands.run("git pull", { + cwd: absoluteRepoDir, + }); + await sandbox.setTimeout(TIMEOUT_MS); + return gitPullOutput; + } catch (e) { + const errorFields = getSandboxErrorFields(e); + logger.error(`Failed to pull latest changes`, { + ...(errorFields && { errorFields }), + ...(e instanceof Error && { + name: e.name, + message: e.message, + stack: e.stack, + }), + }); + return false; + } +}