Skip to content

The open-source app everyone uses to manage agents at work

fix: resolve git credentials with the issue's identity in background jobs (#17515)

Devin Foleycb47dbb2dac9
diff --git a/server/src/__tests__/git-credentials.test.ts b/server/src/__tests__/git-credentials.test.ts
index c286f832b4..e5aee1eb96 100644
--- a/server/src/__tests__/git-credentials.test.ts
+++ b/server/src/__tests__/git-credentials.test.ts
@@ -9,6 +9,7 @@ import {
GIT_CREDENTIAL_TOKEN_ENV_KEY,
buildGitAuthInvocation,
createGitRemoteAuthProvider,
+ createIssueGitRemoteAuthProvider,
describeGitAuthFailure,
isGitHubHttpsRemoteUrl,
scrubGitCredentialText,
@@ -379,3 +380,39 @@ describe("DEFAULT_GITHUB_TOKEN_SECRET_NAMES", () => {
]);
});
});
+
+describe("createIssueGitRemoteAuthProvider", () => {
+ const githubUrl = "https://github.com/paperclipai/private-repo.git";
+ function issueDb(rows: unknown[]) {
+ const where = vi.fn(async () => rows);
+ return { db: { select: vi.fn(() => ({ from: () => ({ where }) })) } as unknown as Db, where };
+ }
+
+ it("resolves credentials with the issue's responsible user and assigned agent", async () => {
+ const { db } = issueDb([{ responsibleUserId: "user-1", assigneeAgentId: "agent-1" }]);
+ const inner = vi.fn(async () => ({ env: {}, configArgs: [], source: "managed_connection" }));
+ const create = vi.fn(() => inner) as unknown as typeof createGitRemoteAuthProvider;
+ const provider = createIssueGitRemoteAuthProvider(db, "company-1", "issue-1", create);
+ await expect(provider(githubUrl)).resolves.toMatchObject({ source: "managed_connection" });
+ expect(create).toHaveBeenCalledWith(db, "company-1", { issueId: "issue-1", responsibleUserId: "user-1", agentId: "agent-1" });
+ expect(inner).toHaveBeenCalledWith(githubUrl);
+ });
+
+ it("looks the issue up once across calls", async () => {
+ const { db } = issueDb([{ responsibleUserId: "user-1", assigneeAgentId: null }]);
+ const inner = vi.fn(async () => null);
+ const create = vi.fn(() => inner) as unknown as typeof createGitRemoteAuthProvider;
+ const provider = createIssueGitRemoteAuthProvider(db, "company-1", "issue-1", create);
+ await provider(githubUrl); await provider(githubUrl);
+ expect(db.select).toHaveBeenCalledTimes(1);
+ expect(create).toHaveBeenCalledTimes(1);
+ expect(inner).toHaveBeenCalledTimes(2);
+ });
+
+ it("falls back to the issue-only context when the issue is missing", async () => {
+ const { db } = issueDb([]);
+ const create = vi.fn(() => vi.fn(async () => null)) as unknown as typeof createGitRemoteAuthProvider;
+ await createIssueGitRemoteAuthProvider(db, "company-1", "issue-1", create)(githubUrl);
+ expect(create).toHaveBeenCalledWith(db, "company-1", { issueId: "issue-1", responsibleUserId: null, agentId: null });
+ });
+});
diff --git a/server/src/services/execution-workspaces.ts b/server/src/services/execution-workspaces.ts
index 63800f66b9..4d83e2c8a9 100644
--- a/server/src/services/execution-workspaces.ts
+++ b/server/src/services/execution-workspaces.ts
@@ -98,7 +98,7 @@ import {
type PullRequestMergeDetailsResolver,
} from "./github-pull-request-merge.js";
import { visibleIssueCondition } from "./issue-visibility.js";
-import { createGitRemoteAuthProvider } from "./git-credentials.js";
+import { createIssueGitRemoteAuthProvider } from "./git-credentials.js";
import { readProjectWorkspaceRuntimeConfig } from "./project-workspace-runtime-config.js";
import { workspaceGitOperationScheduler } from "./workspace-git-operation-scheduler.js";
import { isRuntimeOwnedGitBranch } from "./execution-workspace-branch-ownership.js";
@@ -4113,7 +4113,7 @@ export function executionWorkspaceService(db: Db, opts: ExecutionWorkspaceServic
try { recipe = readGitCacheRestoration(snapshot); }
catch { return { ok: false, code: "rebuild_failed", message: new GitCacheRestorationFailure().message }; }
const freshStart = recipe !== null && isClosedExecutionWorkspaceStatus(snapshot.status) && gitRestorePointExpired(recipe);
- const gitAuth = createGitRemoteAuthProvider(db, snapshot.companyId, { issueId: issue.id });
+ const gitAuth = createIssueGitRemoteAuthProvider(db, snapshot.companyId, issue.id);
const [
{ ensurePersistedExecutionWorkspaceAvailable },
{ workspaceOperationService },
@@ -4168,9 +4168,7 @@ export function executionWorkspaceService(db: Db, opts: ExecutionWorkspaceServic
});
};
},
- resolveGitAuth: createGitRemoteAuthProvider(db, snapshot.companyId, {
- issueId: issue.id,
- }),
+ resolveGitAuth: createIssueGitRemoteAuthProvider(db, snapshot.companyId, issue.id),
});
resolvedBaseCwd = managedWorkspace.cwd;
} catch {
diff --git a/server/src/services/git-credentials.ts b/server/src/services/git-credentials.ts
index 21899e5cef..3fce7fc2b5 100644
--- a/server/src/services/git-credentials.ts
+++ b/server/src/services/git-credentials.ts
@@ -1,6 +1,7 @@
import {
companySecrets,
heartbeatRuns,
+ issues,
companyMemberships,
connectionGrantDelegations,
connectionGrants,
@@ -207,6 +208,19 @@ type GitCredentialSecretsDeps = {
* provider instance so one run performs at most one secret resolution (and writes at most
* one audit event) no matter how many git operations it authenticates.
*/
+export function createIssueGitRemoteAuthProvider(db: Db, companyId: string, issueId: string,
+ create: typeof createGitRemoteAuthProvider = createGitRemoteAuthProvider): GitRemoteAuthProvider {
+ let provider: Promise<GitRemoteAuthProvider> | null = null;
+ return async (remoteUrl: string) => {
+ provider ??= db.select({ responsibleUserId: issues.responsibleUserId, assigneeAgentId: issues.assigneeAgentId }).from(issues)
+ .where(and(eq(issues.id, issueId), eq(issues.companyId, companyId)))
+ .then(([issue]) => create(db, companyId, {
+ issueId, responsibleUserId: issue?.responsibleUserId ?? null, agentId: issue?.assigneeAgentId ?? null,
+ }));
+ return (await provider)(remoteUrl);
+ };
+}
+
export function createGitRemoteAuthProvider(
db: Db,
companyId: string,
diff --git a/server/src/services/git-workspace-expiration.ts b/server/src/services/git-workspace-expiration.ts
index 12cb8e52dd..5fdd85c7f9 100644
--- a/server/src/services/git-workspace-expiration.ts
+++ b/server/src/services/git-workspace-expiration.ts
@@ -6,7 +6,7 @@ import { acquireGitExpirationLocks } from "./git-workspace-expiration-locks.js";
import { and, eq, inArray, ne, sql } from "drizzle-orm";
import { executionWorkspaces, issues, heartbeatRuns, nativeRunFinalizations, environmentLeases,
workspaceRuntimeServices, issueRecoveryActions, projectWorkspaces, withDedicatedDbConnection, type Db } from "@paperclipai/db";
-import { createGitRemoteAuthProvider } from "./git-credentials.js";
+import { createIssueGitRemoteAuthProvider } from "./git-credentials.js";
import type { StorageService } from "../storage/types.js";
import { TASK_DIRECTORY_RETENTION_KEY } from "./isolated-task-directory-snapshot.js";
import { currentRestorePointSnapshot, RestorePointSkip, type RestorePointLimits } from "./git-restore-point.js";
@@ -37,7 +37,7 @@ export function withAnonymousGitFallback(provider: GitRemoteAuthProvider): GitRe
}
function expirationGitAuth(db: Db, workspace: Workspace) {
- return withAnonymousGitFallback(createGitRemoteAuthProvider(db, workspace.companyId, { issueId: workspace.sourceIssueId! }));
+ return withAnonymousGitFallback(createIssueGitRemoteAuthProvider(db, workspace.companyId, workspace.sourceIssueId!));
}
const EXPIRATION_KEY = "gitCacheExpiration";
diff --git a/server/src/services/isolated-repository-rehome.ts b/server/src/services/isolated-repository-rehome.ts
index c8c02f8557..6314f5edcc 100644
--- a/server/src/services/isolated-repository-rehome.ts
+++ b/server/src/services/isolated-repository-rehome.ts
@@ -23,7 +23,7 @@ import { withExecutionWorkspaceLifecycleLock } from "./execution-workspace-lifec
import { loadGitRestorePoint } from "./git-restore-point-storage.js";
import { nestedWorktreeBranchPrefix, readLinkedNestedCheckout, repairMovedNestedRepositoryWorktree, withNestedRepositoryBaseLock } from "./nested-repository-worktrees.js";
import { getStorageService } from "../storage/index.js";
-import { createGitRemoteAuthProvider } from "./git-credentials.js";
+import { createIssueGitRemoteAuthProvider } from "./git-credentials.js";
import { resolvePaperclipInstanceRoot } from "../home-paths.js";
const execFile = promisify(execFileCallback);
@@ -598,7 +598,7 @@ async function worktreeOfClone(worktree: string, cloneReal: string, signal?: Abo
}
function upstreamCheck(db: Db, companyId: string, issueId: string, clone: string, repoUrl: string, signal?: AbortSignal): MissingUpstream {
- const auth = withAnonymousGitFallback(createGitRemoteAuthProvider(db, companyId, { issueId }));
+ const auth = withAnonymousGitFallback(createIssueGitRemoteAuthProvider(db, companyId, issueId));
return (commits, covered) => hasCommitsMissingUpstream(clone, commits, repoUrl, auth, signal, covered);
}
diff --git a/server/src/services/nested-clone-migration.ts b/server/src/services/nested-clone-migration.ts
index eb0e3a87ed..09b4f6663a 100644
--- a/server/src/services/nested-clone-migration.ts
+++ b/server/src/services/nested-clone-migration.ts
@@ -13,7 +13,7 @@ import { GIT_CACHE_RESTORATION_KEY, readGitCacheRestoration, type GitCacheRestor
import { readManagedCheckoutContents, selectRestoredProjectRepositories } from "./git-workspace-contents.js";
import { applyRestorePoint, captureRestorePoint, currentRestorePointSnapshot, importRestorePoint } from "./git-restore-point.js";
import { managedProjectCheckoutName } from "./managed-project-checkout-name.js";
-import { createGitRemoteAuthProvider, type GitRemoteAuthProvider } from "./git-credentials.js";
+import { createIssueGitRemoteAuthProvider, type GitRemoteAuthProvider } from "./git-credentials.js";
import { isNestedWorktreeBranchName, isVerifiedNestedRepositoryBase, moveNestedRepositoryWorktree, nestedRepositoryBaseCandidates, nestedWorktreeBranchPrefix,
readLinkedNestedCheckout, removeStaleNestedRegistrations, trackNestedWorktreeUpstream, withNestedRepositoryBaseLock } from "./nested-repository-worktrees.js";
import { assertSafeBaseConfig, assertTransferredItems, historyKey, linkedCheckoutBases, moveTransferItems, presentCommits, returnTransferItems,
@@ -442,7 +442,7 @@ export async function migrateNestedRepositoryClone(db: Db, snapshot: Workspace,
const base = source.base;
await assertSafeBaseConfig(base, signal);
await fetchBase(base, source.repoUrl, options.resolveGitAuth
- ?? withAnonymousGitFallback(createGitRemoteAuthProvider(db, row!.companyId, { issueId: row!.sourceIssueId! })), signal);
+ ?? withAnonymousGitFallback(createIssueGitRemoteAuthProvider(db, row!.companyId, row!.sourceIssueId!)), signal);
await assertSafeBaseConfig(base, signal);
await assertNoBranchOnlyReflogHistory(inspected, signal);
const planned = await planRefs(inspected, base, prefix, signal);