Skip to content

Commit c39eeb2

Browse files
authored
Merge branch 'main' into fix/mobile-android-composer-keyboard
2 parents e9c0ca5 + 26fffe5 commit c39eeb2

12 files changed

Lines changed: 412 additions & 76 deletions

File tree

-1.04 MB
Binary file not shown.
1.05 MB
Binary file not shown.

‎apps/mobile/package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@
120120
"react": "19.3.0",
121121
"react-dom": "19.3.0",
122122
"react-native": "0.88.0-rc.3",
123-
"react-native-enriched-markdown": "file:deps/react-native-enriched-markdown-1.2.0-t3.11.tgz",
123+
"react-native-enriched-markdown": "file:deps/react-native-enriched-markdown-1.2.0-t3.12.tgz",
124124
"react-native-gesture-handler": "~3.2.1",
125125
"react-native-image-viewing": "^0.2.2",
126126
"react-native-keyboard-controller": "1.22.6",

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

Lines changed: 168 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,23 @@ import { SourceControlProviderKind } from "@t3tools/contracts";
22
import { assert, it } from "@effect/vitest";
33
import * as NodeServices from "@effect/platform-node/NodeServices";
44
import * as DateTime from "effect/DateTime";
5+
import * as Deferred from "effect/Deferred";
56
import * as Effect from "effect/Effect";
7+
import * as Fiber from "effect/Fiber";
68
import * as Layer from "effect/Layer";
79
import * as Option from "effect/Option";
10+
import * as PlatformError from "effect/PlatformError";
11+
import * as PubSub from "effect/PubSub";
812
import * as Redacted from "effect/Redacted";
13+
import * as Stream from "effect/Stream";
14+
import * as TestClock from "effect/testing/TestClock";
915
import { ChildProcessSpawner } from "effect/process";
10-
import { VcsRepositoryDetectionError } from "@t3tools/contracts";
16+
import {
17+
DEFAULT_SERVER_SETTINGS,
18+
VcsProcessSpawnError,
19+
VcsProcessTimeoutError,
20+
VcsRepositoryDetectionError,
21+
} from "@t3tools/contracts";
1122

1223
import * as ServerSettings from "../serverSettings.ts";
1324
import * as ServerConfig from "../config.ts";
@@ -54,6 +65,8 @@ function makeRegistry(input: {
5465
readonly process?: Partial<VcsProcess.VcsProcess["Service"]>;
5566
readonly githubApi?: Partial<GitHubApi.GitHubApi["Service"]>;
5667
readonly gitlab?: Partial<GitLabCli.GitLabCli["Service"]>;
68+
readonly forgejo?: Partial<ForgejoCli.ForgejoCli["Service"]>;
69+
readonly settings?: Partial<ServerSettings.ServerSettingsService["Service"]>;
5770
readonly resolve?: VcsDriverRegistry.VcsDriverRegistry["Service"]["resolve"];
5871
}) {
5972
const driver = {
@@ -118,7 +131,9 @@ function makeRegistry(input: {
118131
Layer.mock(AzureDevOpsPullRequestCli.AzureDevOpsPullRequestCli)({}),
119132
Layer.mock(BitbucketApi.BitbucketApi)({}),
120133
Layer.mock(BitbucketPullRequestApi.BitbucketPullRequestApi)({}),
121-
ServerSettings.ServerSettingsService.layerTest(),
134+
input.settings
135+
? Layer.mock(ServerSettings.ServerSettingsService)(input.settings)
136+
: ServerSettings.ServerSettingsService.layerTest(),
122137
Layer.mock(GitHubPullRequestApi.GitHubPullRequestApi)({}),
123138
Layer.mock(GitHubApi.GitHubApi)({
124139
// No GitHub credential unless a test supplies one, so custom hosts stay unclaimed.
@@ -128,7 +143,10 @@ function makeRegistry(input: {
128143
Layer.mock(GitVcsDriver.GitVcsDriver)({}),
129144
Layer.mock(GitLabCli.GitLabCli)(input.gitlab ?? {}),
130145
Layer.mock(GitLabPullRequestCli.GitLabPullRequestCli)({}),
131-
Layer.mock(ForgejoCli.ForgejoCli)({ listLogins: () => Effect.succeed([]) }),
146+
Layer.mock(ForgejoCli.ForgejoCli)({
147+
listLogins: () => Effect.succeed([]),
148+
...input.forgejo,
149+
}),
132150
Layer.mock(GitCafeApi.GitCafeApi)({}),
133151
Layer.mock(GitCafeCredentials.GitCafeCredentials)({
134152
cliEnv: () => Effect.succeed({}),
@@ -459,3 +477,150 @@ it.effect("skips GitHub discovery for the identity resolver's empty base URL", (
459477
assert.deepStrictEqual(hosts, ["githubenterprise.dev.example.com"]);
460478
}),
461479
);
480+
481+
const notFound = (command: string, cwd: string) =>
482+
new VcsProcessSpawnError({
483+
operation: "test",
484+
command,
485+
cwd,
486+
cause: PlatformError.systemError({ _tag: "NotFound", module: "ChildProcess", method: "spawn" }),
487+
});
488+
489+
const resolveUnknown = (
490+
registry: SourceControlProviderRegistry.SourceControlProviderRegistry["Service"],
491+
repository: string,
492+
) =>
493+
registry.resolveHandle({
494+
cwd: `/${repository}`,
495+
context: {
496+
provider: {
497+
kind: SourceControlProviderKind.make("unknown"),
498+
name: "git.example.internal",
499+
baseUrl: "https://git.example.internal",
500+
},
501+
remoteName: "origin",
502+
remoteUrl: `[email protected]:team/${repository}.git`,
503+
},
504+
});
505+
506+
it.effect("asks once for every checkout of a server no integration claims", () =>
507+
Effect.gen(function* () {
508+
const asked: Array<string> = [];
509+
const release = yield* Deferred.make<void>();
510+
const registry = yield* makeRegistry({
511+
remotes: [],
512+
process: {
513+
run: ({ command, args, cwd }) => {
514+
asked.push([command, ...args].join(" "));
515+
return Deferred.await(release).pipe(Effect.andThen(Effect.fail(notFound(command, cwd))));
516+
},
517+
},
518+
githubApi: {
519+
credential: (host) => {
520+
asked.push(`credential ${host}`);
521+
return Effect.fail(new GitHubCredentials.GitHubNotSignedInError({ host }));
522+
},
523+
},
524+
forgejo: {
525+
listLogins: ({ command = "tea", cwd }) => {
526+
asked.push(command);
527+
const cause = notFound(command, cwd);
528+
return Effect.fail(new ForgejoCli.ForgejoCliError({ command, cwd, detail: "", cause }));
529+
},
530+
},
531+
});
532+
533+
// Three checkouts read while the first probe is still running share it.
534+
const started = yield* Effect.forEach(["a", "b", "c"], () => Deferred.make<void>());
535+
const callers = yield* Effect.forEach(
536+
["a", "b", "c"],
537+
(repository, index) =>
538+
Deferred.succeed(started[index]!, undefined).pipe(
539+
Effect.andThen(resolveUnknown(registry, repository)),
540+
),
541+
{ concurrency: "unbounded" },
542+
).pipe(Effect.forkChild);
543+
yield* Effect.forEach(started, Deferred.await);
544+
yield* Deferred.succeed(release, undefined);
545+
const handles = yield* Fiber.join(callers);
546+
yield* resolveUnknown(registry, "d");
547+
assert.deepStrictEqual(
548+
handles.map((handle) => handle.provider.kind),
549+
["unknown", "unknown", "unknown"],
550+
);
551+
const once = asked.slice();
552+
// glab is asked about the remote's host as the remote spells it.
553+
assert.sameMembers(once, [
554+
"credential git.example.internal",
555+
"glab auth status --hostname Git.Example.Internal",
556+
"fj",
557+
"tea",
558+
]);
559+
560+
// A login made in a terminal sends no signal, so the server is asked again later.
561+
yield* TestClock.adjust("5 minutes");
562+
yield* resolveUnknown(registry, "a");
563+
assert.deepStrictEqual(asked, [...once, ...once]);
564+
}),
565+
);
566+
567+
it.effect("asks again after a check that could not answer", () =>
568+
Effect.gen(function* () {
569+
let asks = 0;
570+
const registry = yield* makeRegistry({
571+
remotes: [],
572+
process: {
573+
run: ({ command, cwd }) => {
574+
asks += 1;
575+
return Effect.fail(
576+
new VcsProcessTimeoutError({ operation: "test", command, cwd, timeoutMs: 1 }),
577+
);
578+
},
579+
},
580+
});
581+
yield* resolveUnknown(registry, "a");
582+
yield* resolveUnknown(registry, "b");
583+
assert.strictEqual(asks, 2);
584+
}),
585+
);
586+
587+
it.effect("asks again once Settings change", () =>
588+
Effect.gen(function* () {
589+
const changes = yield* PubSub.unbounded<typeof DEFAULT_SERVER_SETTINGS>();
590+
const handled = yield* Deferred.make<void>();
591+
let signedIn = false;
592+
const registry = yield* makeRegistry({
593+
remotes: [],
594+
settings: {
595+
subscribeChanges: PubSub.subscribe(changes).pipe(
596+
Effect.map((subscription) =>
597+
// A change is handled once the registry asks for the next one.
598+
Stream.fromSubscription(subscription).pipe(
599+
Stream.flatMap((settings) =>
600+
Stream.concat(
601+
Stream.make(settings),
602+
Stream.fromEffectDrain(Deferred.succeed(handled, undefined)),
603+
),
604+
),
605+
),
606+
),
607+
),
608+
},
609+
process: { run: ({ command, cwd }) => Effect.fail(notFound(command, cwd)) },
610+
githubApi: {
611+
credential: (host) =>
612+
signedIn
613+
? Effect.succeed({ token: Redacted.make("token"), fingerprint: `${host}:fingerprint` })
614+
: Effect.fail(new GitHubCredentials.GitHubNotSignedInError({ host })),
615+
},
616+
});
617+
assert.strictEqual((yield* resolveUnknown(registry, "a")).provider.kind, "unknown");
618+
signedIn = true;
619+
assert.strictEqual((yield* resolveUnknown(registry, "b")).provider.kind, "unknown");
620+
621+
// A token saved for the host in Settings.
622+
yield* PubSub.publish(changes, DEFAULT_SERVER_SETTINGS);
623+
yield* Deferred.await(handled);
624+
assert.strictEqual((yield* resolveUnknown(registry, "c")).provider.kind, "github");
625+
}),
626+
);

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

Lines changed: 73 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@ import * as Cache from "effect/Cache";
33
import * as Context from "effect/Context";
44
import * as Duration from "effect/Duration";
55
import * as Effect from "effect/Effect";
6+
import * as Equal from "effect/Equal";
67
import * as Exit from "effect/Exit";
8+
import * as Hash from "effect/Hash";
79
import * as Layer from "effect/Layer";
10+
import * as Stream from "effect/Stream";
811
import {
912
SourceControlProviderError,
1013
type SourceControlProviderDiscoveryItem,
@@ -18,14 +21,19 @@ import {
1821
probeSourceControlProvider,
1922
refineUnknownRemoteProvider,
2023
type SourceControlProviderDiscoverySpec,
24+
type UnknownRemoteVerdict,
2125
} from "@t3tools/source-control-core/server/discovery";
2226
import * as SourceControlHost from "@t3tools/source-control-core/server/SourceControlHost";
2327

2428
import * as ServerConfig from "../config.ts";
29+
import * as ServerSettings from "../serverSettings.ts";
2530
import * as VcsDriverRegistry from "../vcs/VcsDriverRegistry.ts";
2631

2732
const PROVIDER_DETECTION_CACHE_CAPACITY = 2_048;
2833
const PROVIDER_DETECTION_CACHE_TTL = Duration.seconds(5);
34+
const UNKNOWN_REMOTE_CACHE_CAPACITY = 512;
35+
// Outlasts the one-minute pull request sweep. A login made since is noticed after this.
36+
const UNKNOWN_REMOTE_CACHE_TTL = Duration.minutes(5);
2937

3038
export interface SourceControlProviderRegistration {
3139
readonly kind: SourceControlProviderKind;
@@ -150,6 +158,38 @@ function selectProviderContext(
150158
);
151159
}
152160

161+
/**
162+
* One lookup per server: remotes that differ only in `owner/repo` share a verdict, and callers
163+
* that arrive while it runs wait on it. `cwd` is left out because it only matters when a probe
164+
* fails there, and a failed probe is not kept. Equality ignores `lookup`, so this is not a
165+
* `Data.Class`.
166+
*/
167+
class UnknownRemoteLookup {
168+
readonly key: string;
169+
readonly lookup: Effect.Effect<UnknownRemoteVerdict>;
170+
171+
constructor(
172+
context: SourceControlProvider.SourceControlProviderContext,
173+
lookup: Effect.Effect<UnknownRemoteVerdict>,
174+
) {
175+
this.lookup = lookup;
176+
this.key = [
177+
context.provider.name,
178+
context.provider.baseUrl,
179+
// Strip a trailing `owner/repo`, never the host after `//`, so one-segment remotes stay apart.
180+
context.remoteUrl.trim().replace(/(?<!\/\/[^/]*)[^/:]+\/[^/:]+\/?$/u, ""),
181+
context.requestedHost ?? "",
182+
].join("\n");
183+
}
184+
185+
[Equal.symbol](that: unknown): boolean {
186+
return that instanceof UnknownRemoteLookup && that.key === this.key;
187+
}
188+
[Hash.symbol](): number {
189+
return Hash.string(this.key);
190+
}
191+
}
192+
153193
function bindProviderContext(
154194
provider: SourceControlProvider.SourceControlProvider["Service"],
155195
context: SourceControlProvider.SourceControlProviderContext | null,
@@ -198,6 +238,7 @@ function bindProviderContext(
198238
export const makeWithProviders = Effect.fn("makeSourceControlProviderRegistryWithProviders")(
199239
function* (registrations: ReadonlyArray<SourceControlProviderRegistration>) {
200240
const config = yield* ServerConfig.ServerConfig;
241+
const serverSettings = yield* ServerSettings.ServerSettingsService;
201242
const { process } = yield* SourceControlHost.SourceControlHost;
202243
const vcsRegistry = yield* VcsDriverRegistry.VcsDriverRegistry;
203244
const providers = new Map<
@@ -209,6 +250,36 @@ export const makeWithProviders = Effect.fn("makeSourceControlProviderRegistryWit
209250
const get: SourceControlProviderRegistry["Service"]["get"] = (kind) =>
210251
Effect.succeed(providers.get(kind) ?? unsupportedProvider(kind));
211252

253+
// "No integration holds this host" is the common verdict and the one worth keeping. A verdict
254+
// reached while some probe failed is handed to the callers waiting on it, then dropped.
255+
const unknownRemoteCache = yield* Cache.makeWith(
256+
(request: UnknownRemoteLookup) => request.lookup,
257+
{
258+
capacity: UNKNOWN_REMOTE_CACHE_CAPACITY,
259+
timeToLive: (exit) =>
260+
Exit.isSuccess(exit) && exit.value.settled ? UNKNOWN_REMOTE_CACHE_TTL : Duration.zero,
261+
},
262+
);
263+
// A token saved or a host toggled in Settings must show on the next read, not after the TTL.
264+
const settingsChanges = yield* serverSettings.subscribeChanges;
265+
yield* Stream.runForEach(settingsChanges, () => Cache.invalidateAll(unknownRemoteCache)).pipe(
266+
Effect.forkScoped,
267+
);
268+
269+
const refineContext = (
270+
cwd: string,
271+
context: SourceControlProvider.SourceControlProviderContext | null,
272+
): Effect.Effect<SourceControlProvider.SourceControlProviderContext | null> =>
273+
context === null || context.provider.kind !== "unknown"
274+
? Effect.succeed(context)
275+
: Cache.get(
276+
unknownRemoteCache,
277+
new UnknownRemoteLookup(
278+
context,
279+
refineUnknownRemoteProvider({ specs: discoverySpecs, process, cwd, context }),
280+
),
281+
).pipe(Effect.map(({ provider }) => (provider ? { ...context, provider } : context)));
282+
212283
const detectProviderContext = Effect.fn("SourceControlProviderRegistry.detectProviderContext")(
213284
function* (cwd: string) {
214285
const handle = yield* vcsRegistry.resolve({ cwd }).pipe(
@@ -235,14 +306,7 @@ export const makeWithProviders = Effect.fn("makeSourceControlProviderRegistryWit
235306
}),
236307
),
237308
);
238-
const context = selectProviderContext(remotes.remotes);
239-
240-
return yield* refineUnknownRemoteProvider({
241-
specs: discoverySpecs,
242-
process,
243-
cwd,
244-
context,
245-
});
309+
return yield* refineContext(cwd, selectProviderContext(remotes.remotes));
246310
},
247311
);
248312

@@ -258,12 +322,7 @@ export const makeWithProviders = Effect.fn("makeSourceControlProviderRegistryWit
258322
const resolveHandle: SourceControlProviderRegistry["Service"]["resolveHandle"] = (input) =>
259323
(input.context === undefined
260324
? Cache.get(providerContextCache, input.cwd)
261-
: refineUnknownRemoteProvider({
262-
specs: discoverySpecs,
263-
process,
264-
cwd: input.cwd,
265-
context: input.context,
266-
})
325+
: refineContext(input.cwd, input.context)
267326
).pipe(
268327
Effect.map((context) => {
269328
const kind = context?.provider.kind ?? SourceControlProviderKind.make("unknown");

0 commit comments

Comments
 (0)