Skip to content

Commit 7c2023f

Browse files
committed
fix(source-control): retain detection caches for unrelated settings
1 parent bea9408 commit 7c2023f

2 files changed

Lines changed: 86 additions & 28 deletions

File tree

‎apps/server/src/sourceControl/SourceControlProviderRegistry.test.ts‎

Lines changed: 81 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -70,22 +70,25 @@ function makeRegistry(input: {
7070
readonly gitlab?: Partial<GitLabCli.GitLabCli["Service"]>;
7171
readonly forgejo?: Partial<ForgejoCli.ForgejoCli["Service"]>;
7272
readonly settings?: Partial<ServerSettings.ServerSettingsService["Service"]>;
73+
readonly listRemotes?: VcsDriver.VcsDriver["Service"]["listRemotes"];
7374
readonly resolve?: VcsDriverRegistry.VcsDriverRegistry["Service"]["resolve"];
7475
}) {
7576
const driver = {
76-
listRemotes: () =>
77-
Effect.succeed({
78-
remotes: input.remotes.map((remote) => ({
79-
...remote,
80-
pushUrl: Option.none(),
81-
isPrimary: remote.name === "origin",
77+
listRemotes:
78+
input.listRemotes ??
79+
(() =>
80+
Effect.succeed({
81+
remotes: input.remotes.map((remote) => ({
82+
...remote,
83+
pushUrl: Option.none(),
84+
isPrimary: remote.name === "origin",
85+
})),
86+
freshness: {
87+
source: "live-local" as const,
88+
observedAt: TEST_EPOCH,
89+
expiresAt: Option.none(),
90+
},
8291
})),
83-
freshness: {
84-
source: "live-local" as const,
85-
observedAt: TEST_EPOCH,
86-
expiresAt: Option.none(),
87-
},
88-
}),
8992
} satisfies Partial<VcsDriver.VcsDriver["Service"]>;
9093

9194
const layerRegistry = Layer.mock(VcsDriverRegistry.VcsDriverRegistry)({
@@ -691,18 +694,40 @@ it.effect("asks again after a check that could not answer", () =>
691694
}),
692695
);
693696

694-
it.effect("asks again once Settings change", () =>
697+
it.effect("keeps both detection caches until normalized host mappings change", () =>
695698
Effect.gen(function* () {
696-
const changes = yield* PubSub.unbounded<typeof DEFAULT_SERVER_SETTINGS>();
697-
const handled = yield* Deferred.make<void>();
698-
let signedIn = false;
699+
const changes = yield* PubSub.unbounded<{
700+
settings: typeof DEFAULT_SERVER_SETTINGS;
701+
handled: Deferred.Deferred<void>;
702+
}>();
703+
let listings = 0;
704+
let probes = 0;
699705
const registry = yield* makeRegistry({
700706
remotes: [],
707+
listRemotes: () =>
708+
Effect.sync(() => {
709+
listings += 1;
710+
return {
711+
remotes: [
712+
{
713+
name: "origin",
714+
url: "https://code.example.test/team/repo.git",
715+
pushUrl: Option.none(),
716+
isPrimary: true,
717+
},
718+
],
719+
freshness: {
720+
source: "live-local" as const,
721+
observedAt: TEST_EPOCH,
722+
expiresAt: Option.none(),
723+
},
724+
};
725+
}),
701726
settings: {
702727
subscribeChanges: PubSub.subscribe(changes).pipe(
703728
Effect.map((subscription) =>
704729
Stream.fromSubscription(subscription).pipe(
705-
Stream.flatMap((settings) =>
730+
Stream.flatMap(({ settings, handled }) =>
706731
Stream.concat(
707732
Stream.make(settings),
708733
Stream.fromEffectDrain(Deferred.succeed(handled, undefined)),
@@ -714,18 +739,46 @@ it.effect("asks again once Settings change", () =>
714739
},
715740
process: { run: ({ command, cwd }) => Effect.fail(notFound(command, cwd)) },
716741
githubApi: {
717-
credential: (host) =>
718-
signedIn
719-
? Effect.succeed({ token: Redacted.make("token"), fingerprint: `${host}:fingerprint` })
720-
: Effect.fail(new GitHubCredentials.GitHubNotSignedInError({ host })),
742+
credential: (host) => {
743+
probes += 1;
744+
return Effect.fail(new GitHubCredentials.GitHubNotSignedInError({ host }));
745+
},
721746
},
722747
});
723-
assert.strictEqual((yield* resolveUnknown(registry, "a")).provider.kind, "unknown");
724-
signedIn = true;
725-
assert.strictEqual((yield* resolveUnknown(registry, "b")).provider.kind, "unknown");
726-
727-
yield* PubSub.publish(changes, DEFAULT_SERVER_SETTINGS);
728-
yield* Deferred.await(handled);
729-
assert.strictEqual((yield* resolveUnknown(registry, "c")).provider.kind, "github");
748+
const publish = Effect.fnUntraced(function* (settings: typeof DEFAULT_SERVER_SETTINGS) {
749+
const handled = yield* Deferred.make<void>();
750+
yield* PubSub.publish(changes, { settings, handled });
751+
yield* Deferred.await(handled);
752+
});
753+
const resolve = (cwd = "/repo") => registry.resolveHandle({ cwd });
754+
assert.strictEqual((yield* resolve()).provider.kind, "unknown");
755+
assert.deepStrictEqual([listings, probes], [1, 1]);
756+
for (const worktreesDirectory of ["/worktrees/a", "/worktrees/b"]) {
757+
yield* publish({ ...DEFAULT_SERVER_SETTINGS, worktreesDirectory });
758+
yield* resolve();
759+
assert.deepStrictEqual([listings, probes], [1, 1]);
760+
}
761+
// A fresh checkout misses the context cache but must reuse settled host discovery.
762+
yield* resolve("/other");
763+
assert.deepStrictEqual([listings, probes], [2, 1]);
764+
yield* publish({
765+
...DEFAULT_SERVER_SETTINGS,
766+
sourceControlHostOverrides: {
767+
"code.example.test": { kind: "gitlab", baseUrl: "https://code.example.test" },
768+
},
769+
});
770+
assert.strictEqual((yield* resolve()).provider.kind, "gitlab");
771+
assert.deepStrictEqual([listings, probes], [3, 1]);
772+
yield* publish({
773+
...DEFAULT_SERVER_SETTINGS,
774+
sourceControlHostOverrides: {
775+
"CODE.EXAMPLE.TEST": { kind: "gitlab", baseUrl: "https://code.example.test/" },
776+
},
777+
});
778+
assert.strictEqual((yield* resolve()).provider.kind, "gitlab");
779+
assert.deepStrictEqual([listings, probes], [3, 1]);
780+
yield* publish(DEFAULT_SERVER_SETTINGS);
781+
assert.strictEqual((yield* resolve()).provider.kind, "unknown");
782+
assert.deepStrictEqual([listings, probes], [4, 2]);
730783
}),
731784
);

‎apps/server/src/sourceControl/SourceControlProviderRegistry.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import type {} from "@t3tools/contracts";
1717
import {
1818
detectSourceControlProviderFromRemoteUrl,
1919
sourceControlHostOverrideForRemoteUrl,
20+
sourceControlHostOverridesKey,
2021
} from "@t3tools/shared/sourceControl";
2122

2223
import * as BuiltInDrivers from "./builtInDrivers.ts";
@@ -262,6 +263,7 @@ export const makeWithProviders = Effect.fn("makeSourceControlProviderRegistryWit
262263
);
263264
const settingsChanges = yield* serverSettings.subscribeChanges;
264265
let hostOverrides = yield* readHostOverrides(serverSettings.getSettings);
266+
let hostOverridesKey = sourceControlHostOverridesKey(hostOverrides);
265267

266268
const refineContext = (
267269
cwd: string,
@@ -325,6 +327,9 @@ export const makeWithProviders = Effect.fn("makeSourceControlProviderRegistryWit
325327
yield* Stream.runForEach(settingsChanges, (settings) =>
326328
Effect.gen(function* () {
327329
hostOverrides = settings.sourceControlHostOverrides;
330+
const nextKey = sourceControlHostOverridesKey(hostOverrides);
331+
if (nextKey === hostOverridesKey) return;
332+
hostOverridesKey = nextKey;
328333
yield* Cache.invalidateAll(unknownRemoteCache);
329334
yield* Cache.invalidateAll(providerContextCache);
330335
}),

0 commit comments

Comments
 (0)