refactor(reference): consolidate Git metadata cleanup

This commit is contained in:
morluto
2026-10-11 06:30:56 +08:00
parent 54e2890b30
commit 279c942fe1
2 changed files with 145 additions and 128 deletions
+59 -101
View File
@@ -4,10 +4,8 @@ import { join, resolve } from "node:path";
import { err, ok, type Result } from "../domain/result.js";
import type { ArtifactResourceScope } from "../artifacts/ArtifactResourceScope.js";
import { ArtifactReaderFailure } from "../artifacts/ArtifactReader.js";
import type { AnalysisCleanupObservation } from "../domain/analysisErrorBase.js";
import {
readRegularFile,
readRegularFileText,
RegularFileCleanupFailure,
retryRegularFileCleanup,
} from "./RegularFileRead.js";
@@ -50,42 +48,6 @@ export const readReferenceSourceVcs = async (
return ok({ kind: "git", head, dirty: null });
} catch (cause: unknown) {
if (cause instanceof ArtifactReaderFailure) throw cause;
if (cause instanceof RegularFileCleanupFailure) {
const retry = await retryRegularFileCleanup(cause, resources);
if (retry.cleanup !== undefined) {
const primary =
cause.outcome.kind === "failed"
? cause.outcome.cause
: cause.cleanupCause;
const code = isAborted(signal) ? "cancelled" : "io";
return err({
tag: "reference-source-reader",
code,
message: `Git metadata could not be read safely: ${errorMessage(primary)}`,
cleanup: retry.cleanup,
cause: primary,
});
}
if (retry.outcome.kind === "completed")
return err({
tag: "reference-source-reader",
code: isAborted(signal) ? "cancelled" : "io",
message: isAborted(signal)
? "Git metadata read cancelled"
: `Git metadata close failed: ${errorMessage(cause.cleanupCause)}`,
cause: cause.cleanupCause,
});
if (retry.outcome.kind === "failed") {
return isAborted(signal)
? err({
tag: "reference-source-reader",
code: "cancelled",
message: "Git metadata read cancelled",
cause: retry.outcome.cause,
})
: ok(unknownVcs());
}
}
return ok(unknownVcs());
}
});
@@ -115,26 +77,65 @@ const resolveSourceHead = async (
// isomorphic-git is loaded on first use so CLI and MCP startup skip it.
const { resolveRef } = await import("isomorphic-git");
let failedRead: { readonly cause: unknown } | undefined;
const cleanupFailures: {
readonly failure: RegularFileCleanupFailure;
readonly cleanup?: NonNullable<ArtifactReaderFailure["cleanup"]>;
}[] = [];
const cleanupFailures: ArtifactReaderFailure[] = [];
const readMetadataBytes = async (path: string): Promise<Buffer> => {
try {
return await readRegularFile(path, { signal });
} catch (cause: unknown) {
if (!(cause instanceof RegularFileCleanupFailure)) throw cause;
const retry = await retryRegularFileCleanup(cause, resources);
if (
retry.outcome.kind === "failed" &&
errorCode(retry.outcome.cause) !== "ENOENT"
)
failedRead ??= { cause: retry.outcome.cause };
if (
retry.cleanup === undefined &&
retry.outcome.kind === "failed" &&
!isAborted(signal)
)
throw retry.outcome.cause;
const primary =
retry.outcome.kind === "failed"
? retry.outcome.cause
: cause.cleanupCause;
throw new ArtifactReaderFailure(
isAborted(signal) ? "cancelled" : "io",
`Git metadata could not be read safely: ${errorMessage(primary)}`,
{
cause: primary,
...(retry.cleanup === undefined ? {} : { cleanup: retry.cleanup }),
},
);
}
};
const readMetadataText = async (path: string): Promise<string> =>
(await readMetadataBytes(path)).toString("utf8");
const throwCleanupFailures = (primary: unknown): never => {
const cleanup = mergeCleanup(
cleanupFailures.flatMap((failure) =>
failure.cleanup === undefined ? [] : [failure.cleanup],
),
const observations = cleanupFailures.flatMap((failure) =>
failure.cleanup === undefined ? [] : [failure.cleanup],
);
throw new ArtifactReaderFailure(
isAborted(signal) ? "cancelled" : "io",
`Git metadata could not be read safely: ${errorMessage(primary)}`,
{
cause: new AggregateError(
[primary, ...cleanupFailures.map((item) => item.failure)],
[primary, ...cleanupFailures],
"Git metadata reads and cleanup failed",
{ cause: primary },
),
...(cleanup === undefined ? {} : { cleanup }),
...(observations.length === 0
? {}
: {
cleanup: {
reason: observations.map(({ reason }) => reason).join("; "),
resources: [
...new Set(
observations.flatMap(({ resources }) => resources),
),
],
},
}),
},
);
};
@@ -157,26 +158,15 @@ const resolveSourceHead = async (
if (path === undefined)
throw new TypeError("Git filesystem read requires a path");
try {
const bytes = await readRegularFile(path, { signal });
const bytes = await readMetadataBytes(path);
const encoding =
typeof options === "string" ? options : options?.encoding;
return encoding === undefined || encoding === null
? bytes
: bytes.toString(encoding);
} catch (cause: unknown) {
if (cause instanceof RegularFileCleanupFailure) {
const retry = await retryRegularFileCleanup(cause, resources);
cleanupFailures.push({
failure: cause,
...(retry.cleanup === undefined
? {}
: { cleanup: retry.cleanup }),
});
if (
retry.outcome.kind === "failed" &&
errorCode(retry.outcome.cause) !== "ENOENT"
)
failedRead ??= { cause: retry.outcome.cause };
if (cause instanceof ArtifactReaderFailure) {
cleanupFailures.push(cause);
} else if (errorCode(cause) !== "ENOENT") failedRead ??= { cause };
throw cause;
}
@@ -197,19 +187,14 @@ const resolveSourceHead = async (
throw cause;
}
if (cleanupFailures.length > 0)
throwCleanupFailures(
failedRead?.cause ?? cleanupFailurePrimary(cleanupFailures[0]),
);
throwCleanupFailures(failedRead?.cause ?? cleanupFailures[0]?.cause);
signal?.throwIfAborted();
if (failedRead !== undefined) throw failedRead.cause;
return head;
};
const dotgit = join(root, ".git");
if (!(await lstat(dotgit)).isFile()) return readRef({ ref: "HEAD" });
const pointer = (await readRegularFileText(dotgit, { signal })).replace(
/\r?\n$/u,
"",
);
const pointer = (await readMetadataText(dotgit)).replace(/\r?\n$/u, "");
if (!pointer.startsWith("gitdir: ") || pointer.length === 8)
throw new Error("Invalid Git directory pointer");
const gitdir = resolve(root, pointer.slice(8));
@@ -217,17 +202,16 @@ const resolveSourceHead = async (
try {
commonDirectory = resolve(
gitdir,
(
await readRegularFileText(join(gitdir, "commondir"), { signal })
).replace(/\r?\n$/u, ""),
(await readMetadataText(join(gitdir, "commondir"))).replace(
/\r?\n$/u,
"",
),
);
} catch (cause: unknown) {
if (errorCode(cause) !== "ENOENT") throw cause;
return readRef({ gitdir, ref: "HEAD" });
}
const head = (
await readRegularFileText(join(gitdir, "HEAD"), { signal })
).trim();
const head = (await readMetadataText(join(gitdir, "HEAD"))).trim();
if (!head.startsWith("ref: ")) return readRef({ gitdir, ref: "HEAD" });
const ref = head.slice(5);
const privateRef = ["refs/bisect/", "refs/rewritten/", "refs/worktree/"].some(
@@ -239,32 +223,6 @@ const resolveSourceHead = async (
});
};
const cleanupFailurePrimary = (
failure:
| {
readonly failure: RegularFileCleanupFailure;
}
| undefined,
): unknown =>
failure?.failure.outcome.kind === "failed"
? failure.failure.outcome.cause
: failure?.failure.cleanupCause;
const mergeCleanup = (
observations: readonly AnalysisCleanupObservation[],
): AnalysisCleanupObservation | undefined => {
if (observations.length === 0) return undefined;
const first = observations[0];
if (first === undefined) return undefined;
return observations.slice(1).reduce<AnalysisCleanupObservation>(
(merged, next) => ({
reason: `${merged.reason}; ${next.reason}`,
resources: [...new Set([...merged.resources, ...next.resources])],
}),
first,
);
};
const errorCode = (cause: unknown): string | undefined =>
typeof cause === "object" && cause !== null && "code" in cause
? String(cause.code)