Files
PaperClipAI/server/src/services/managed-repository-checkout.ts
T
commitperclip[bot] 8b7b2a0701 fix: Recognize Git mount-boundary absence diagnostics in workspace checks (#21048)
> [!NOTE]
> Opened by Prospector's automated issue-fix pipeline. A maintainer
reviews and decides; nothing here merges on its own.

> [!WARNING]
> This change crosses a trust boundary. See Risks.

## Thinking Path

- Issue #20525 reports a defect in paperclipai/paperclip.
- Root cause, as the fixing agent read it: Both callers recognized only
the ordinary Git absence diagnostic. Preserve the earlier exact boundary
matcher and all existing exit, signal, environment, and filesystem
checks.
- This pull request: Recognize Git mount-boundary absence diagnostics in
workspace checks

## What Changed

- `server/src/services/git-mount-boundary-message.ts`: Retain the shared
complete-diagnostic matcher, which rejects partial and appended output.
- `server/src/services/managed-repository-checkout.ts`: Retain boundary
recognition without changing ordinary-diagnostic behavior or fallback
guards.
- `server/src/services/heartbeat/workspaces.ts`: Retain boundary
recognition and ancestor metadata inspection.
- `server/src/__tests__/git-mount-boundary.test.ts`: Add root mount,
CRLF, and embedded-line-break cases through both callers. Confirmed six
failures with boundary recognition disabled; restored implementation
passes this suite, both neighboring suites, and typecheck.

## Verification

- Reproducing test(s), added in this PR:
`server/src/__tests__/git-mount-boundary.test.ts`
- They fail on the base commit and pass with the fix, each twice in a
fresh sandbox.
- The repository's compile command passes with the fix applied.
- 10 existing test file(s) near the change passed.
- The full test suite passes except for the 16 file(s) that already fail
on the base commit.

## Risks

- Written by an automated pipeline, not a person: review it as you would
an outside contribution.
- Risk tier of the touched paths: 2.
- It crosses a trust boundary. Check each crossing against what the
issue asked for, and against who asked:
- validation (server/src/services/heartbeat/workspaces.ts:860): Broadens
the accepted Git absence diagnostics to include the mount-boundary
message when classifying a checkout failure as an expected
non-repository result.
- validation (server/src/services/managed-repository-checkout.ts:265):
Allows the mount-boundary diagnostic to bypass a previously thrown Git
error when plainTaskDirectory and the existing exit, signal, and
environment conditions hold.

## Model Used

- opus (Anthropic Claude, via Claude Code), writing and checking the
change in an isolated sandbox.
- opus (Anthropic Claude, via Claude Code), reviewing it independently.

## Linked Issues

Fixes #20525

#20525 also mentions, without asking this fix for it:
- Non-Git directories in close readiness. (The report explicitly
identifies merged #19553 as handling 'a different caller'; this patch
does not modify close readiness.)
- Initial managed-clone readiness. (The report says '#2977 concerns
initial managed-clone readiness, rather than this diagnostic matcher.')
- CI jobs 131047332629 and 131040975860 fail; fix the change only if it
causes the failures. (This earlier-round conditional comment supplies no
demonstrated matcher defect. .github/workflows/pr-trusted.yml:1801-1804
asserts aggregate shard status, while
server/src/services/tool-access/catalog/health.ts:222 reports MCP health
failures; neither changed here. Historical job causation cannot be)

A search of paperclipai/paperclip's open and closed pull requests found
no other pull request that names #20525.

## Checklist

- [x] I have included a thinking path that traces from project context
to this change
- [x] I have specified the model used (with version and capability
details)
- [ ] I have checked ROADMAP.md and confirmed this PR does not duplicate
planned core work
- [x] I have searched GitHub for duplicate or related PRs and linked
them above
- [x] I have either (a) linked existing issues with 'Fixes: #' / 'Closes
#' / 'Refs #' OR (b) described the issue in-PR following the relevant
issue template
- [x] I have not referenced internal/instance-local Paperclip issues or
links (only public GitHub '#NNN' / 'github.com/paperclipai/paperclip'
URLs)
- [ ] My branch name describes the change (e.g. 'docs/...', 'fix/...')
and contains no internal Paperclip ticket id or instance-derived details
- [x] I have run tests locally and they pass
- [x] I have added or updated tests where applicable
- [ ] I have updated relevant documentation to reflect my changes
- [x] I have considered and documented any risks above
- [ ] All Paperclip CI gates are green
- [ ] Greptile is 5/5 with no open P2s, recommendations, or follow-ups
- [ ] I will address all Greptile and reviewer comments before
requesting merge

<!-- prospector:issue-fix v1 issue=20525 base=156453fe7c67
report=513d758246cb2ac5 -->

Co-authored-by: commitperclip[bot] <284812584+commitperclip[bot]@users.noreply.github.com>
2026-11-13 12:59:22 -06:00

276 lines
14 KiB
TypeScript

import { retainCurrentWorkspaceSource } from "./workspace-source-lock.js";
import { isGitMountBoundaryMessage } from "./git-mount-boundary-message.js";
import fs from "node:fs/promises";
import path from "node:path";
import { execFile as execFileCallback } from "node:child_process";
import { promisify } from "node:util";
import { PROJECT_REPOSITORIES_DIR, readGitWorkspaceSnapshot, disposeGitWorkspaceSnapshot } from "@paperclipai/adapter-utils/git-workspace-sync";
import { captureDirectorySnapshot, disposeDirectorySnapshot, mergeDirectoryWithBaseline } from "@paperclipai/adapter-utils/workspace-restore-merge";
import { sanitizeRuntimeServiceBaseEnv } from "./runtime-service-env.js";
import { describeGitAuthFailure, scrubGitCredentialText, type GitRemoteAuthProvider } from "./git-credentials.js";
import { classifyGitCloneFailure, GitConnectionFailureError, readGitConnectionFailure } from "./git-connection-failure.js";
import { isWorkspaceGitScanError, WorkspaceGitScanError } from "./workspace-git-operation-scheduler.js";
import { measureRunPhase, recordRepositoryObjectCacheDetail } from "./execution-preparation-timing.js";
import { resolveRepositoryObjectCache, readRepositoryObjectCache, publishRepositoryObjectCache } from "./repository-object-cache.js";
const execFile = promisify(execFileCallback);
const MANAGED_WORKSPACE_GIT_CLONE_TIMEOUT_MS = 10 * 60 * 1000;
type ManagedRepositoryPreparationFailure = { code: string | number | null; timedOut: boolean };
const preparationFailures = new WeakMap<Error, ManagedRepositoryPreparationFailure>();
export function readManagedRepositoryPreparationFailure(error: unknown): ManagedRepositoryPreparationFailure | null {
const failure = error instanceof Error ? preparationFailures.get(error) : undefined;
return failure ? { ...failure } : null;
}
function managedRepositoryPreparationError(message: string, error: unknown): Error {
const wrapped = new Error(message);
const retained = readManagedRepositoryPreparationFailure(error);
if (retained) preparationFailures.set(wrapped, retained);
else if (error instanceof Error) {
const details = error as Error & { code?: unknown; killed?: unknown };
const code = typeof details.code === "number" && Number.isSafeInteger(details.code) ? details.code
: typeof details.code === "string" && /^(?:E[A-Z0-9_]{1,63}|ABORT_ERR)$/.test(details.code) ? details.code : null;
const timedOut = code !== "ERR_CHILD_PROCESS_STDIO_MAXBUFFER" && (details.killed === true || code === "ETIMEDOUT"
|| error.name === "AbortError" || error.name === "TimeoutError");
if (code !== null || timedOut) preparationFailures.set(wrapped, { code, timedOut });
}
return wrapped;
}
async function copyManagedGitObjects(cwd: string, repoUrl: string, source: { cwd: string; commits: readonly string[] }) {
const git = (directory: string, args: string[]) => execFile("git", ["-C", directory, ...args], {
env: { ...sanitizeRuntimeServiceBaseEnv(process.env), GIT_TERMINAL_PROMPT: "0" },
timeout: MANAGED_WORKSPACE_GIT_CLONE_TIMEOUT_MS,
});
const present = (directory: string, commit: string) => git(directory, ["cat-file", "-e", `${commit}^{commit}`]).then(() => true, () => false);
if (source.commits.some(commit => !/^[a-f0-9]{40}$/.test(commit))) throw new Error("Invalid managed Git object source");
await git(cwd, ["init", "--quiet"]);
await git(cwd, ["remote", "add", "origin", repoUrl]);
for (const commit of new Set(source.commits)) {
if (await present(cwd, commit) || !await present(source.cwd, commit)) continue;
await git(cwd, ["fetch", "--no-tags", "--no-recurse-submodules", "--no-write-fetch-head", "--no-auto-maintenance", "--", source.cwd, commit]);
}
}
/** The requested branch, tag, or commit is absent from a complete fresh clone. */
export class RepositoryRefNotFoundError extends Error {
constructor(message: string, readonly requestedRef: string) {
super(message);
this.name = "RepositoryRefNotFoundError";
}
}
/**
* True only when Git reported an unknown ref and the clone positively cannot
* resolve it. `rev-parse --verify --quiet` exits 1 for an unknown name; any
* other failure (auth, corruption, timeout) is not evidence of a missing ref.
*/
async function isMissingRef(cloneCwd: string, ref: string, checkoutError: unknown): Promise<boolean> {
const stderr = String((checkoutError as { stderr?: unknown } | null)?.stderr ?? "");
if (!/did not match any file\(s\) known to git|invalid reference/.test(stderr)) return false;
for (const candidate of [`${ref}^{commit}`, `refs/remotes/origin/${ref}^{commit}`]) {
const exitCode = await execFile("git", ["-C", cloneCwd, "rev-parse", "--verify", "--quiet", "--end-of-options", candidate], { timeout: 30_000 })
.then(() => 0, (error: { code?: unknown }) => error.code);
if (exitCode !== 1) return false;
}
return true;
}
export async function materializeManagedProjectWorkspace(
cwd: string,
input: {
repoUrl: string | null;
repoRef?: string | null;
localSource?: string | null;
gitObjectSource?: { cwd: string; commits: readonly string[] };
resolveGitAuth?: GitRemoteAuthProvider | null;
beforeCopy?: (cloneCwd: string) => Promise<void>;
beforePublish?: (cloneCwd: string) => Promise<void>;
stagingParent?: string;
objectCacheCompanyDirectory?: string;
},
): Promise<{ cwd: string; warning: string | null }> {
await retainCurrentWorkspaceSource(cwd);
if (input.localSource) await retainCurrentWorkspaceSource(input.localSource);
if (input.gitObjectSource) await retainCurrentWorkspaceSource(input.gitObjectSource.cwd);
if (input.localSource && input.gitObjectSource) throw new Error("Conflicting managed checkout sources");
await fs.mkdir(path.dirname(cwd), { recursive: true });
const stats = await fs.stat(cwd).catch(() => null);
if (!input.repoUrl) {
if (!stats) {
await fs.mkdir(cwd, { recursive: true });
}
return { cwd, warning: null };
}
const hasAdoptableGitDir = () => measureRunPhase("workspace.checkout.lookup", () =>
fs
.stat(path.resolve(cwd, ".git"))
.then((entry) => entry.isDirectory())
.catch(() => false));
if (await hasAdoptableGitDir()) {
return { cwd, warning: null };
}
if (stats) {
const entries = await fs.readdir(cwd).catch(() => []);
if (entries.length > 0) {
return {
cwd,
warning: `Managed workspace path "${cwd}" already exists but is not a git checkout. Using it as-is.`,
};
}
await fs.rm(cwd, { recursive: true, force: true });
}
let auth = input.resolveGitAuth && !input.localSource && !input.gitObjectSource
? await measureRunPhase("workspace.checkout.git_auth", () => input.resolveGitAuth!(input.repoUrl!))
: null;
const cloneTmpDir = await fs.mkdtemp(input.stagingParent
? path.join(input.stagingParent, `${path.basename(cwd)}.clone-`)
: `${cwd}.clone-`);
const cloneSource = input.localSource ?? input.repoUrl;
let objectCache = input.objectCacheCompanyDirectory && !input.localSource && !input.gitObjectSource
? await measureRunPhase("workspace.checkout.cache_lookup", async () => {
const cache = await resolveRepositoryObjectCache(input.objectCacheCompanyDirectory!, input.repoUrl!, auth);
if (!cache) recordRepositoryObjectCacheDetail({ outcome: "disabled" });
return cache;
}) : null;
const reference = objectCache ? await measureRunPhase("workspace.checkout.cache_lookup", async () => {
const cached = await readRepositoryObjectCache(objectCache!);
recordRepositoryObjectCacheDetail({ outcome: cached ? "hit" : "miss" });
return cached;
}) : null;
try {
await input.beforeCopy?.(cloneTmpDir);
try {
const clone = (referencePath: string | null) => measureRunPhase("workspace.checkout.git_clone", async () => {
recordRepositoryObjectCacheDetail({ referenceSupplied: referencePath !== null });
if (input.gitObjectSource) return copyManagedGitObjects(cloneTmpDir, input.repoUrl!, input.gitObjectSource);
await execFile(
"git",
[...(auth?.configArgs ?? []), ...(referencePath ? ["-c", "repack.updateServerInfo=false"] : []), "clone", "--no-hardlinks", ...(referencePath ? ["--reference-if-able", referencePath, "--dissociate"] : []), "--", cloneSource, cloneTmpDir],
{
env: {
...sanitizeRuntimeServiceBaseEnv(process.env),
GIT_TERMINAL_PROMPT: "0",
...(auth?.env ?? {}),
},
timeout: MANAGED_WORKSPACE_GIT_CLONE_TIMEOUT_MS,
},
);
});
try { await clone(reference); }
catch (error) {
if (!reference) throw error;
for (const name of await fs.readdir(cloneTmpDir)) await fs.rm(path.join(cloneTmpDir, name), { recursive: true, force: true });
objectCache = null;
auth = input.resolveGitAuth ? await input.resolveGitAuth(input.repoUrl) : null;
await clone(null);
}
} catch (error) {
const connectionFailure = !input.localSource && !input.gitObjectSource ? classifyGitCloneFailure(input.repoUrl, error) : null;
if (connectionFailure) throw new GitConnectionFailureError(
error instanceof Error ? error.message : "Git clone failed", connectionFailure,
);
throw error;
}
if (objectCache && !reference) await measureRunPhase("workspace.checkout.cache_publish", () => publishRepositoryObjectCache(objectCache!, cloneTmpDir));
if (input.localSource) {
const snapshot = await readGitWorkspaceSnapshot(input.localSource, false);
if (!snapshot) throw new Error("Configured repository folder is not a Git checkout");
let baseline;
try {
baseline = await captureDirectorySnapshot(cloneTmpDir, { exclude: [".git", ".paperclip-runtime", PROJECT_REPOSITORIES_DIR], ignoredPaths: snapshot.ignoredPaths, diskBacked: true });
await mergeDirectoryWithBaseline({ baseline, sourceDir: input.localSource, targetDir: cloneTmpDir });
} finally {
if (baseline) await disposeDirectorySnapshot(baseline);
await disposeGitWorkspaceSnapshot(snapshot);
}
await execFile("git", ["-C", cloneTmpDir, "remote", "set-url", "origin", input.repoUrl], { timeout: 10_000 });
} else if (input.repoRef) {
const repoRef = input.repoRef;
try {
await measureRunPhase("workspace.checkout.git_ref", () => execFile("git", ["-C", cloneTmpDir, "checkout", repoRef], {
// Untranslated Git messages keep missing-ref classification locale independent.
timeout: MANAGED_WORKSPACE_GIT_CLONE_TIMEOUT_MS, env: { ...process.env, LC_ALL: "C", LANGUAGE: "C" },
}));
} catch (error) {
// Only a ref that a complete fresh clone cannot resolve is a missing ref;
// every other checkout failure keeps its original cause.
if (!await isMissingRef(cloneTmpDir, repoRef, error)) throw error;
throw new RepositoryRefNotFoundError(
`Requested ref "${repoRef}" does not exist in the repository. Request the repository again with an existing branch, tag, or commit.`,
repoRef,
);
}
}
if (input.gitObjectSource) await execFile("git", ["-C", cloneTmpDir, "remote", "set-url", "origin", input.repoUrl], { timeout: 10_000 });
await input.beforePublish?.(cloneTmpDir);
} catch (error) {
await fs
.rm(cloneTmpDir, { recursive: true, force: true })
.catch(() => undefined);
const reason = error instanceof Error ? error.message : String(error);
const authNote = describeGitAuthFailure({
error: reason,
used: auth ? { source: auth.source, secretName: auth.secretName } : null,
});
const message = scrubGitCredentialText(
`Failed to prepare managed checkout for "${input.repoUrl}" at "${cwd}": ${reason}${authNote ? ` ${authNote}` : ""}`,
);
if (isWorkspaceGitScanError(error)) throw new WorkspaceGitScanError(error.code, message);
if (error instanceof RepositoryRefNotFoundError) throw new RepositoryRefNotFoundError(message, error.requestedRef);
const connectionFailure = readGitConnectionFailure(error);
if (connectionFailure) throw new GitConnectionFailureError(message, connectionFailure);
throw managedRepositoryPreparationError(message, error);
}
try {
await measureRunPhase("workspace.checkout.publish", () => fs.rename(cloneTmpDir, cwd));
} catch (renameError) {
await fs
.rm(cloneTmpDir, { recursive: true, force: true })
.catch(() => undefined);
if (await hasAdoptableGitDir()) {
return { cwd, warning: null };
}
const reason =
renameError instanceof Error ? renameError.message : String(renameError);
throw managedRepositoryPreparationError(
`Failed to move managed checkout into place at "${cwd}": ${reason}`,
renameError,
);
}
return { cwd, warning: null };
}
export async function ensureManagedRepositoriesIgnored(cwd: string, options: { plainTaskDirectory?: boolean } = {}): Promise<void> {
const gitEntry = await fs.lstat(path.join(cwd, ".git")).catch(() => null);
if (!gitEntry) return;
if (gitEntry.isSymbolicLink()) throw new Error("Workspace Git metadata must not be a symlink");
let excludePath: string;
try {
const result = await execFile("git", ["-C", cwd, "rev-parse", "--git-path", "info/exclude"], {
timeout: 10_000, env: { ...process.env, LC_ALL: "C" },
});
excludePath = path.resolve(cwd, result.stdout.trim());
} catch (error) {
const failure = error as { code?: unknown; signal?: unknown; stderr?: unknown };
if (options.plainTaskDirectory && failure.code === 128 && failure.signal == null &&
(failure.stderr === "fatal: not a git repository (or any of the parent directories): .git\n" ||
isGitMountBoundaryMessage(failure.stderr)) &&
!process.env.GIT_DIR && !process.env.GIT_WORK_TREE && !process.env.GIT_COMMON_DIR) return;
throw error;
}
const exclude = await fs.readFile(excludePath, "utf8").catch(() => "");
if (!exclude.split(/\r?\n/).includes(`/${PROJECT_REPOSITORIES_DIR}/`)) {
await fs.mkdir(path.dirname(excludePath), { recursive: true });
await fs.appendFile(excludePath, `\n/${PROJECT_REPOSITORIES_DIR}/\n`);
}
}