The best way to control Claude Code, Codex and more from any computer or phone
fix(server): SSH host aliases no longer split a repository into separate groups (#38852)
Julius Marminge committed
Nov 18, 2026 at 19:47 UTC
44431935c5ec4aa675c26941f852b0a41485a66a
2 files changed
+113
-10
apps/server/src/project/RepositoryIdentityResolver.test.ts
+47
@@ -5,6 +5,7 @@ import { expect, it } from "@effect/vitest";
5
import * as Effect from "effect/Effect";
6
import * as FileSystem from "effect/FileSystem";
7
import * as Path from "effect/Path";
8
+import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
9
import * as HostProcess from "@t3tools/shared/HostProcess";
10
import { vi } from "vite-plus/test";
11
@@ -81,6 +82,52 @@ it.layer(NodeServices.layer)("RepositoryIdentityResolverLive", (it) => {
82
}).pipe(Effect.provide(RepositoryIdentityResolver.layer)),
83
);
84
85
+ it.effect("keys a remote behind an SSH host alias by the host ssh config resolves it to", () =>
86
+ Effect.gen(function* () {
87
+ const fileSystem = yield* FileSystem.FileSystem;
88
+ const aliasDir = yield* fileSystem.makeTempDirectoryScoped({
89
+ prefix: "t3-repository-identity-ssh-alias-test-",
90
+ });
91
+ const plainDir = yield* fileSystem.makeTempDirectoryScoped({
92
+ prefix: "t3-repository-identity-ssh-plain-test-",
93
+ });
94
+
95
+ yield* git(aliasDir, ["init"]);
96
+ yield* git(aliasDir, ["remote", "add", "origin", "git@github.com-personal:owner/repo.git"]);
97
+ yield* git(plainDir, ["init"]);
98
+ yield* git(plainDir, ["remote", "add", "origin", "git@github.com:owner/repo.git"]);
99
+
100
+ const runner = yield* ProcessRunner.ProcessRunner;
101
+ // Stands in for `Host github.com-personal` / `HostName github.com` in ~/.ssh/config.
102
+ const resolver = yield* RepositoryIdentityResolver.make().pipe(
103
+ Effect.provideService(ProcessRunner.ProcessRunner, {
104
+ run: (input) =>
105
+ input.command === "ssh" && input.args.includes("-G")
106
+ ? Effect.succeed({
107
+ stdout: input.args.some((arg) => arg.includes("github.com-personal"))
108
+ ? "user git\nhostname github.com\nport 22\n"
109
+ : `hostname ${input.args.at(-1) ?? ""}\nport 22\n`,
110
+ stderr: "",
111
+ code: ChildProcessSpawner.ExitCode(0),
112
+ timedOut: false,
113
+ stdoutTruncated: false,
114
+ stderrTruncated: false,
115
+ stdoutInvalidUtf8: false,
116
+ stderrInvalidUtf8: false,
117
+ })
118
+ : runner.run(input),
119
+ }),
120
+ );
121
+
122
+ const aliasIdentity = yield* resolver.resolve(aliasDir);
123
+ const plainIdentity = yield* resolver.resolve(plainDir);
124
+
125
+ expect(plainIdentity?.canonicalKey).toBe("github.com/owner/repo");
126
+ expect(aliasIdentity?.canonicalKey).toBe(plainIdentity?.canonicalKey);
127
+ expect(aliasIdentity?.locator.remoteUrl).toBe("git@github.com-personal:owner/repo.git");
128
+ }).pipe(Effect.provide(ProcessRunner.layer)),
129
+ );
130
+
131
it.effect("keeps a literal .git in an Azure DevOps repository name", () =>
132
Effect.gen(function* () {
133
const fileSystem = yield* FileSystem.FileSystem;
apps/server/src/project/RepositoryIdentityResolver.ts
+66
-10
@@ -2,6 +2,7 @@ import type { RepositoryIdentity, SourceControlProviderError } from "@t3tools/co
2
import { parseGitRemoteHost } from "@t3tools/source-control-core/shared/definition";
3
import { repositoryPathFromRemoteUrl } from "@t3tools/source-control-core/server/SourceControlProvider";
4
import { canonicalRepositoryPath, normalizeGitRemoteUrl } from "@t3tools/shared/git";
5
+import { isSshRemoteUrl } from "@t3tools/shared/sourceControl";
6
import * as Cache from "effect/Cache";
7
import * as Context from "effect/Context";
8
import * as Data from "effect/Data";
@@ -132,12 +133,58 @@ function repositoryPathOf(canonicalKey: string, remoteUrl: string): string {
133
);
134
}
135
136
+// A leading `-` on the user or host would be read by ssh as an option.
137
+const SSH_REMOTE = /^((?:ssh:\/\/)?)((?:[^-@/:][^@/:]*@)?)([A-Za-z0-9][A-Za-z0-9._-]*)([:/].*)$/su;
138
+const SSH_HOST_NAME = /^[a-z0-9][a-z0-9.-]*$/u;
139
+const IPV4 = /^[\d.]+$/u;
140
+
141
+// Keys an SSH remote by the host its ssh config alias (`Host github.com-personal`) points at.
142
+// A dotted host may be a forge whose SSH endpoint has another name, so only a public forge
143
+// replaces it. `ssh -G` prints the configuration it would use and opens no connection.
144
+const sshResolvedRemoteUrl = Effect.fn("RepositoryIdentityResolver.sshResolvedRemoteUrl")(
145
+ function* (remoteUrl: string) {
146
+ const match = isSshRemoteUrl(remoteUrl) ? SSH_REMOTE.exec(remoteUrl.trim()) : null;
147
+ if (!match) return remoteUrl;
148
+ const [, scheme = "", user = "", host = "", rest = ""] = match;
149
+ if (sourceControlDefinitions.findByPublicHost(host)) return remoteUrl;
150
+ const processRunner = yield* ProcessRunner.ProcessRunner;
151
+ const result = yield* processRunner
152
+ .run({
153
+ command: "ssh",
154
+ args: ["-G", `${user}${host}`],
155
+ timeout: Duration.seconds(5),
156
+ maxOutputBytes: 64_000,
157
+ outputMode: "truncate",
158
+ timeoutBehavior: "timedOutResult",
159
+ })
160
+ .pipe(Effect.option);
161
+ if (result._tag === "None" || result.value.code !== 0) return remoteUrl;
162
+ const hostName = result.value.stdout
163
+ .split("\n")
164
+ .find((line) => line.startsWith("hostname "))
165
+ ?.slice("hostname ".length)
166
+ .trim()
167
+ .toLowerCase();
168
+ if (
169
+ hostName === undefined ||
170
+ !SSH_HOST_NAME.test(hostName) ||
171
+ IPV4.test(hostName) ||
172
+ hostName === host.toLowerCase() ||
173
+ (host.includes(".") && !sourceControlDefinitions.findByPublicHost(hostName))
174
+ ) {
175
+ return remoteUrl;
176
+ }
177
+ return `${scheme}${user}${hostName}${rest}`;
178
+ },
179
+);
180
+
181
function buildRepositoryOrigin(
182
originUrl: string | undefined,
183
+ originKeyUrl: string | undefined,
184
canonicalKey: string,
185
): RepositoryIdentity["origin"] {
186
if (!originUrl) return undefined;
140
- const originKey = sourceControlDefinitions.repositoryKeyFromRemote(originUrl);
187
+ const originKey = sourceControlDefinitions.repositoryKeyFromRemote(originKeyUrl ?? originUrl);
188
if (originKey === canonicalKey) return undefined;
189
const displayName = repositoryPathOf(originKey, originUrl);
190
return { canonicalKey: originKey, remoteUrl: originUrl, ...(displayName ? { displayName } : {}) };
@@ -146,16 +193,18 @@ function buildRepositoryOrigin(
193
function buildRepositoryIdentity(input: {
194
readonly remoteName: string;
195
readonly remoteUrl: string;
196
+ readonly keyUrl: string;
197
readonly originUrl: string | undefined;
198
+ readonly originKeyUrl: string | undefined;
199
readonly rootPath: string;
200
}): RepositoryIdentity {
152
- const canonicalKey = sourceControlDefinitions.repositoryKeyFromRemote(input.remoteUrl);
201
+ const canonicalKey = sourceControlDefinitions.repositoryKeyFromRemote(input.keyUrl);
202
const sourceControlProvider = sourceControlDefinitions.recognizeRemote(input.remoteUrl);
203
const repositoryPath = repositoryPathOf(canonicalKey, input.remoteUrl);
204
const repositoryPathSegments = repositoryPath.split("/").filter((segment) => segment.length > 0);
205
const [owner] = repositoryPathSegments;
206
const repositoryName = repositoryPathSegments.at(-1);
158
- const origin = buildRepositoryOrigin(input.originUrl, canonicalKey);
207
+ const origin = buildRepositoryOrigin(input.originUrl, input.originKeyUrl, canonicalKey);
208
209
return {
210
canonicalKey,
@@ -298,13 +347,20 @@ const resolveRepositoryIdentityFromCacheKey = Effect.fn(
347
? resolvedResult.value.stdout
348
: "";
349
const remote = pickPrimaryRemote(remotes, resolved);
301
- return remote
302
- ? buildRepositoryIdentity({
303
- ...remote,
304
- originUrl: remotes.get("origin"),
305
- rootPath: cacheKey.rootPath,
306
- })
307
- : null;
350
+ if (!remote) return null;
351
+ const originUrl = remotes.get("origin");
352
+ const keyUrl = yield* sshResolvedRemoteUrl(remote.remoteUrl);
353
+ const originKeyUrl =
354
+ originUrl === undefined || originUrl === remote.remoteUrl
355
+ ? keyUrl
356
+ : yield* sshResolvedRemoteUrl(originUrl);
357
+ return buildRepositoryIdentity({
358
+ ...remote,
359
+ keyUrl,
360
+ originUrl,
361
+ originKeyUrl,
362
+ rootPath: cacheKey.rootPath,
363
+ });
364
});
365
366
export const make = Effect.fn("RepositoryIdentityResolver.make")(function* (