Skip to content

Commit b0ef34e

Browse files
committed
refactor(server): type repository identity lookup failures
Replace the generic NoSuchElementError with a tagged RepositoryIdentityLookupError that records the stage, directory, exit code and underlying process failure. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_01WvDgDwRRG8xvtoABQ8KwtC
1 parent b8e299e commit b0ef34e

1 file changed

Lines changed: 47 additions & 16 deletions

File tree

‎apps/server/src/project/RepositoryIdentityResolver.ts‎

Lines changed: 47 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ import {
44
normalizeGitRemoteUrl,
55
} from "@t3tools/shared/git";
66
import * as Cache from "effect/Cache";
7-
import * as Cause from "effect/Cause";
87
import * as Context from "effect/Context";
98
import * as Duration from "effect/Duration";
109
import * as Effect from "effect/Effect";
1110
import * as Exit from "effect/Exit";
1211
import * as Layer from "effect/Layer";
12+
import * as Schema from "effect/Schema";
1313

1414
import * as ProcessRunner from "../processRunner.ts";
1515

@@ -20,6 +20,22 @@ const DEFAULT_REPOSITORY_IDENTITY_CACHE_CAPACITY = 512;
2020
const DEFAULT_POSITIVE_CACHE_TTL = Duration.minutes(10);
2121
const DEFAULT_NEGATIVE_CACHE_TTL = Duration.minutes(5);
2222

23+
// Lookup failures stay uncached so the next resolve retries them.
24+
class RepositoryIdentityLookupError extends Schema.TaggedError<RepositoryIdentityLookupError>()(
25+
"RepositoryIdentityLookupError",
26+
{
27+
stage: Schema.Literals(["root", "remote"]),
28+
cwd: Schema.String,
29+
exitCode: Schema.optional(Schema.NullOr(Schema.Number)),
30+
timedOut: Schema.optional(Schema.Boolean),
31+
cause: Schema.optional(Schema.Defect()),
32+
},
33+
) {
34+
override get message(): string {
35+
return `Git ${this.stage} lookup failed in '${this.cwd}'`;
36+
}
37+
}
38+
2339
export interface RepositoryIdentityResolverOptions {
2440
readonly cacheCapacity?: number;
2541
readonly positiveCacheTtl?: Duration.Input;
@@ -111,19 +127,25 @@ const resolveRepositoryIdentityCacheKey = Effect.fn("RepositoryIdentityResolver.
111127
env: { LC_ALL: "C" },
112128
timeoutBehavior: "timedOutResult",
113129
})
114-
.pipe(Effect.option);
115-
if (topLevelResult._tag === "None" || topLevelResult.value.code !== 0) {
130+
.pipe(
131+
Effect.mapError(
132+
(cause) => new RepositoryIdentityLookupError({ stage: "root", cwd, cause }),
133+
),
134+
);
135+
if (topLevelResult.code !== 0) {
116136
// Remember git's "not a repository" verdict; retry timeouts and other failures.
117-
if (
118-
topLevelResult._tag === "Some" &&
119-
/not a git repository/i.test(topLevelResult.value.stderr)
120-
) {
137+
if (!topLevelResult.timedOut && /not a git repository/i.test(topLevelResult.stderr)) {
121138
return null;
122139
}
123-
return yield* new Cause.NoSuchElementError("Git root lookup failed");
140+
return yield* new RepositoryIdentityLookupError({
141+
stage: "root",
142+
cwd,
143+
exitCode: topLevelResult.code,
144+
timedOut: topLevelResult.timedOut,
145+
});
124146
}
125147

126-
const candidate = topLevelResult.value.stdout.trim();
148+
const candidate = topLevelResult.stdout.trim();
127149
return candidate.length > 0 ? candidate : null;
128150
},
129151
);
@@ -134,7 +156,7 @@ const resolveRepositoryIdentityFromCacheKey = Effect.fn(
134156
cacheKey: string,
135157
): Effect.fn.Return<
136158
RepositoryIdentity | null,
137-
Cause.NoSuchElementError,
159+
RepositoryIdentityLookupError,
138160
ProcessRunner.ProcessRunner
139161
> {
140162
const processRunner = yield* ProcessRunner.ProcessRunner;
@@ -144,13 +166,22 @@ const resolveRepositoryIdentityFromCacheKey = Effect.fn(
144166
args: ["-C", cacheKey, "remote", "-v"],
145167
timeoutBehavior: "timedOutResult",
146168
})
147-
.pipe(Effect.option);
148-
if (remoteResult._tag === "None" || remoteResult.value.code !== 0) {
169+
.pipe(
170+
Effect.mapError(
171+
(cause) => new RepositoryIdentityLookupError({ stage: "remote", cwd: cacheKey, cause }),
172+
),
173+
);
174+
if (remoteResult.code !== 0) {
149175
// A repository without remotes exits 0 with no output; failures retry.
150-
return yield* new Cause.NoSuchElementError("Git remote lookup failed");
176+
return yield* new RepositoryIdentityLookupError({
177+
stage: "remote",
178+
cwd: cacheKey,
179+
exitCode: remoteResult.code,
180+
timedOut: remoteResult.timedOut,
181+
});
151182
}
152183

153-
const remote = pickPrimaryRemote(parseRemoteFetchUrls(remoteResult.value.stdout));
184+
const remote = pickPrimaryRemote(parseRemoteFetchUrls(remoteResult.stdout));
154185
return remote ? buildRepositoryIdentity({ ...remote, rootPath: cacheKey }) : null;
155186
});
156187

@@ -164,7 +195,7 @@ export const make = Effect.fn("RepositoryIdentityResolver.make")(function* (
164195
const repositoryRootCache = yield* Cache.makeWith<
165196
string,
166197
string | null,
167-
Cause.NoSuchElementError
198+
RepositoryIdentityLookupError
168199
>(
169200
(cwd) =>
170201
resolveRepositoryIdentityCacheKey(cwd).pipe(
@@ -185,7 +216,7 @@ export const make = Effect.fn("RepositoryIdentityResolver.make")(function* (
185216
const repositoryIdentityCache = yield* Cache.makeWith<
186217
string,
187218
RepositoryIdentity | null,
188-
Cause.NoSuchElementError
219+
RepositoryIdentityLookupError
189220
>(
190221
(cacheKey) =>
191222
resolveRepositoryIdentityFromCacheKey(cacheKey).pipe(

0 commit comments

Comments
 (0)