Skip to content

Commit 6d7c17f

Browse files
committed
fix(source-control): preserve fresh reads across account changes
1 parent 9cdd251 commit 6d7c17f

12 files changed

Lines changed: 616 additions & 74 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import type { SourceControlDriver } from "@t3tools/source-control-core/server/dr
2828
import * as Undici from "@effect/platform-node/Undici";
2929
import * as Effect from "effect/Effect";
3030
import * as Layer from "effect/Layer";
31-
import { FetchHttpClient } from "effect/http";
31+
import * as FetchHttpClient from "effect/http/FetchHttpClient";
3232

3333
import * as ServerSourceControlHost from "./ServerSourceControlHost.ts";
3434

‎packages/source-control-azure-devops/src/server/AzureDevOpsPullRequestCli.test.ts‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1008,7 +1008,7 @@ layer("AzureDevOpsPullRequestCli.layer", (it) => {
10081008
json({
10091009
changeEntries: [
10101010
{ changeType: "add", item: { path: "/huge.bin", objectId: "8f80" } },
1011-
{ changeType: "add", item: { path: "/DEMO.md", objectId: "0ca4" } },
1011+
{ changeType: "add", item: { path: "/docs/%scope\nDEMO.md", objectId: "0ca4" } },
10121012
],
10131013
}),
10141014
),
@@ -1029,7 +1029,7 @@ layer("AzureDevOpsPullRequestCli.layer", (it) => {
10291029
const provider = yield* AzureDevOpsPullRequestProvider.make;
10301030

10311031
const slice = yield* provider.getDiff({
1032-
cwd: "/w",
1032+
cwd: "/w\n%repo",
10331033
repository: "web",
10341034
host: "dev.azure.com",
10351035
number: 42,
@@ -1039,6 +1039,10 @@ layer("AzureDevOpsPullRequestCli.layer", (it) => {
10391039
expect(slice.patch).toContain("diff --git a/huge.bin b/huge.bin");
10401040
// And the file behind it still renders, which is the point of giving up on one file.
10411041
expect(slice.patch).toContain("+hello");
1042+
for (const [call] of mockedExecute.mock.calls) {
1043+
expect(call.cwd).toBe("/w\n%repo");
1044+
}
1045+
expect(argsOfCall(4)).toContain("path=/docs/%scope\nDEMO.md");
10421046
}),
10431047
);
10441048

‎packages/source-control-azure-devops/src/server/AzureDevOpsPullRequestProvider.ts‎

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -162,11 +162,14 @@ export const make = Effect.gen(function* () {
162162
// Made once with the provider, which the registry builds once, so this is the whole build's
163163
// allowance rather than one request's.
164164
const diffSpawns = yield* Semaphore.make(MAX_DIFF_SPAWNS);
165+
const cacheKey = (...parts: ReadonlyArray<string | number>) =>
166+
parts.map((part) => encodeURIComponent(String(part))).join("\n");
165167
// Diff slices and context expansion read the same immutable blobs. Bound both count and size.
166168
const itemContents = yield* Cache.makeWith(
167169
(key: string) => {
168-
const [cwd = "", project = "", repository = "", commit = "", ...pathParts] = key.split("\n");
169-
const path = pathParts.join("\n");
170+
const [cwd = "", project = "", repository = "", commit = "", path = ""] = key
171+
.split("\n")
172+
.map(decodeURIComponent);
170173
return diffSpawns.withPermits(1)(
171174
cli.readItemContent({ cwd, location: { project, repository }, commit, path }),
172175
);
@@ -182,8 +185,12 @@ export const make = Effect.gen(function* () {
182185
const readItemContent = (input: Parameters<typeof cli.readItemContent>[0]) =>
183186
Cache.get(
184187
itemContents,
185-
[input.cwd, input.location.project, input.location.repository, input.commit, input.path].join(
186-
"\n",
188+
cacheKey(
189+
input.cwd,
190+
input.location.project,
191+
input.location.repository,
192+
input.commit,
193+
input.path,
187194
),
188195
);
189196

@@ -257,7 +264,7 @@ export const make = Effect.gen(function* () {
257264
*/
258265
const pullRequestReads = yield* Cache.makeWith(
259266
(key: string) => {
260-
const [cwd = "", number = ""] = key.split("\n");
267+
const [cwd = "", number = ""] = key.split("\n").map(decodeURIComponent);
261268
return cli
262269
.getPullRequest({ cwd, number: Number(number) })
263270
.pipe(
@@ -270,11 +277,11 @@ export const make = Effect.gen(function* () {
270277
);
271278

272279
const readPullRequest = (input: { readonly cwd: string; readonly number: number }) =>
273-
Cache.get(pullRequestReads, `${input.cwd}\n${input.number}`);
280+
Cache.get(pullRequestReads, cacheKey(input.cwd, input.number));
274281

275282
/** Run after a write, so a read asked for once it lands cannot join one that started before. */
276283
const forgetRead = (input: { readonly cwd: string; readonly number: number }) =>
277-
Cache.invalidate(pullRequestReads, `${input.cwd}\n${input.number}`);
284+
Cache.invalidate(pullRequestReads, cacheKey(input.cwd, input.number));
278285

279286
const locationOf = (input: { readonly cwd: string; readonly number: number }) => {
280287
const held = knownLocation(input);
@@ -284,17 +291,18 @@ export const make = Effect.gen(function* () {
284291
};
285292

286293
const iterationKey = (scope: IterationScope, iterationId?: number) =>
287-
[
294+
cacheKey(
288295
scope.cwd,
289296
scope.location.project,
290297
scope.location.repository,
291298
scope.number,
292299
...(iterationId === undefined ? [] : [iterationId]),
293-
].join("\n");
300+
);
294301

295302
const fromIterationKey = (key: string) => {
296-
const [cwd = "", project = "", repository = "", number = "", iterationId = ""] =
297-
key.split("\n");
303+
const [cwd = "", project = "", repository = "", number = "", iterationId = ""] = key
304+
.split("\n")
305+
.map(decodeURIComponent);
298306
return {
299307
cwd,
300308
location: { project, repository },
@@ -304,7 +312,11 @@ export const make = Effect.gen(function* () {
304312
};
305313

306314
const changesKey = (scope: IterationScope, iteration: AzureDevOpsIteration) =>
307-
[iterationKey(scope, iteration.id), iteration.headCommit, iteration.mergeBaseCommit].join("\n");
315+
[
316+
iterationKey(scope, iteration.id),
317+
encodeURIComponent(iteration.headCommit),
318+
encodeURIComponent(iteration.mergeBaseCommit),
319+
].join("\n");
308320

309321
/**
310322
* The pushes a pull request has had, shared by overlapping reads the way a pull request read is.

‎packages/source-control-forgejo/src/server/ForgejoCli.ts‎

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ export interface ForgejoApiInput extends ForgejoRepositoryInput {
132132
readonly path: string;
133133
readonly method?: "GET" | "POST" | "PUT" | "PATCH" | "DELETE";
134134
readonly body?: unknown;
135+
readonly freshCredentials?: boolean;
135136
}
136137

137138
export class ForgejoCli extends Context.Service<
@@ -662,6 +663,7 @@ export const make = Effect.gen(function* () {
662663
(key: string) => {
663664
const [cwd = "", reference, repository, host, remoteUrl, baseUrl, requestedHost, scope] = key
664665
.split("\n")
666+
.map(decodeURIComponent)
665667
.map((value) => value || undefined);
666668
return resolveTarget(
667669
{ cwd, reference, repository, host, remoteUrl, providerBaseUrl: baseUrl, requestedHost },
@@ -680,16 +682,27 @@ export const make = Effect.gen(function* () {
680682
input.context?.provider.baseUrl,
681683
input.context?.requestedHost,
682684
hostOnly ? "host" : "",
683-
].join("\n");
685+
]
686+
.map((part) => encodeURIComponent(part ?? ""))
687+
.join("\n");
684688
const resolveRepository = (input: ForgejoRepositoryInput) => Cache.get(targets, targetKey(input));
685689
const api = Effect.fn("ForgejoCli.api")(function* (input: ForgejoApiInput) {
686-
const repository = yield* Cache.get(
687-
targets,
688-
targetKey(
689-
input,
690-
input.path.replace(/^\/+/, "") === "user" && (!input.method || input.method === "GET"),
691-
),
692-
);
690+
const hostOnly =
691+
input.path.replace(/^\/+/, "") === "user" && (!input.method || input.method === "GET");
692+
const repository = yield* input.freshCredentials
693+
? resolveTarget(
694+
{
695+
cwd: input.cwd,
696+
reference: input.reference,
697+
repository: input.repository,
698+
host: input.host,
699+
remoteUrl: input.context?.remoteUrl,
700+
providerBaseUrl: input.context?.provider.baseUrl,
701+
requestedHost: input.context?.requestedHost,
702+
},
703+
hostOnly,
704+
)
705+
: Cache.get(targets, targetKey(input, hostOnly));
693706
const stdin =
694707
input.body === undefined
695708
? undefined

‎packages/source-control-forgejo/src/server/ForgejoPullRequestProvider.ts‎

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -150,32 +150,36 @@ export const make = Effect.gen(function* () {
150150
return { items, truncated: true };
151151
});
152152
// Share pending reads only: credentials and repository permissions can change between requests.
153+
const cacheKey = (...parts: ReadonlyArray<string | number | undefined>) =>
154+
parts.map((part) => encodeURIComponent(String(part ?? ""))).join("\n");
153155
const pulls = yield* Cache.makeWith(
154156
(key: string) => {
155-
const [cwd = "", repository = "", host = "", number = ""] = key.split("\n");
157+
const [cwd = "", repository = "", host = "", number = ""] = key
158+
.split("\n")
159+
.map(decodeURIComponent);
156160
const ref = { cwd, repository, host, number: Number(number) };
157161
return read({ ...ref, path: pullPath(ref) }, ForgejoPullRequest);
158162
},
159163
{ capacity: 64, timeToLive: () => 0 },
160164
);
161165
const getPull = (input: ProviderRepositoryRef & { readonly number: number }) =>
162-
Cache.get(pulls, [input.cwd, input.repository, input.host, input.number].join("\n"));
166+
Cache.get(pulls, cacheKey(input.cwd, input.repository, input.host, input.number));
163167
const write = (input: ForgejoCli.ForgejoApiInput) =>
164168
request(input).pipe(
165169
Effect.onExit(() => Cache.invalidateAll(pulls)),
166170
Effect.asVoid,
167171
);
168172
const repos = yield* Cache.makeWith(
169173
(key: string) => {
170-
const [cwd = "", repository = "", host = ""] = key.split("\n");
174+
const [cwd = "", repository = "", host = ""] = key.split("\n").map(decodeURIComponent);
171175
const ref = { cwd, repository, host };
172176
return read({ ...ref, path: repoPath(ref) }, ForgejoRepository);
173177
},
174178
{ capacity: 32, timeToLive: () => 0 },
175179
);
176180
const viewers = yield* Cache.makeWith(
177181
(key: string) => {
178-
const [cwd = "", repository, host] = key.split("\n");
182+
const [cwd = "", repository, host] = key.split("\n").map(decodeURIComponent);
179183
return read(
180184
{ cwd, ...(repository ? { repository } : {}), ...(host ? { host } : {}), path: "user" },
181185
ForgejoUser,
@@ -184,12 +188,12 @@ export const make = Effect.gen(function* () {
184188
{ capacity: 32, timeToLive: () => 0 },
185189
);
186190
const getRepo = (input: ProviderRepositoryRef) =>
187-
Cache.get(repos, [input.cwd, input.repository, input.host].join("\n"));
191+
Cache.get(repos, cacheKey(input.cwd, input.repository, input.host));
188192
const getViewer = (input: {
189193
readonly cwd: string;
190194
readonly repository?: string;
191195
readonly host?: string;
192-
}) => Cache.get(viewers, [input.cwd, input.repository, input.host].join("\n"));
196+
}) => Cache.get(viewers, cacheKey(input.cwd, input.repository, input.host));
193197
const permissions = (
194198
repo: typeof ForgejoRepository.Type,
195199
pr: typeof ForgejoPullRequest.Type,
@@ -220,8 +224,14 @@ export const make = Effect.gen(function* () {
220224
const getPermissions = Effect.fn("ForgejoPullRequestProvider.getPermissions")(function* (
221225
input: ProviderRepositoryRef & { readonly number: number },
222226
) {
227+
// A write must not join a detail read started before the account or access changed.
228+
const fresh = { ...input, freshCredentials: true };
223229
const [repo, pr, viewer] = yield* Effect.all(
224-
[getRepo(input), getPull(input), getViewer(input)],
230+
[
231+
read({ ...fresh, path: repoPath(input) }, ForgejoRepository),
232+
read({ ...fresh, path: pullPath(input) }, ForgejoPullRequest),
233+
read({ ...fresh, path: "user" }, ForgejoUser).pipe(Effect.map((user) => user.login)),
234+
],
225235
{ concurrency: 3 },
226236
);
227237
return permissions(repo, pr, viewer);

‎packages/source-control-forgejo/src/server/ForgejoSourceControlProvider.test.ts‎

Lines changed: 55 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ it.effect("refreshes Forgejo viewer permissions immediately after credentials ch
3737
let account = "alice";
3838
const requests: string[] = [];
3939
const renewals: string[] = [];
40+
let holdViewer: Effect.Effect<void> = Effect.void;
4041
return Effect.gen(function* () {
4142
const cli = yield* ForgejoCli.make;
4243
const provider = yield* ForgejoPullRequestProvider.make.pipe(
@@ -47,14 +48,22 @@ it.effect("refreshes Forgejo viewer permissions immediately after credentials ch
4748
assert.deepStrictEqual(alice.verdicts, ["comment"]);
4849
assert.include(alice.actions, "merge");
4950
assert.strictEqual(alice.labels, true);
51+
const started = yield* Deferred.make<void>();
52+
const release = yield* Deferred.make<void>();
53+
holdViewer = Deferred.succeed(started, undefined).pipe(Effect.andThen(Deferred.await(release)));
54+
const detail = yield* provider.getChangeRequest(input).pipe(Effect.forkChild);
55+
yield* Deferred.await(started);
5056
account = "bob";
57+
holdViewer = Effect.void;
5158
const bob = yield* provider.getViewerPermissions(input);
5259
assert.deepStrictEqual(bob.verdicts, ["comment", "approve", "request-changes"]);
5360
assert.deepStrictEqual(bob.actions, []);
5461
assert.strictEqual(bob.labels, false);
5562
assert.strictEqual(bob.requestReviewers, false);
56-
assert.strictEqual(requests.length, 6);
63+
assert.strictEqual(requests.length, 9);
5764
assert.deepStrictEqual(renewals, ["alice", "bob"]);
65+
yield* Deferred.succeed(release, undefined);
66+
assert.deepStrictEqual((yield* Fiber.join(detail)).viewerPermissions?.verdicts, ["comment"]);
5867
}).pipe(
5968
Effect.provideService(
6069
FileSystem.FileSystem,
@@ -90,9 +99,11 @@ it.effect("refreshes Forgejo viewer permissions immediately after credentials ch
9099
merged_at: null,
91100
labels: [],
92101
}
93-
: { full_name: "acme/web", permissions: { push: account === "alice", admin: false } };
94-
return Effect.succeed(
95-
HttpClientResponse.fromWeb(request, new Response(encodeJson(response))),
102+
: request.url.includes("/statuses/")
103+
? []
104+
: { full_name: "acme/web", permissions: { push: account === "alice", admin: false } };
105+
return (request.url.endsWith("/user") ? holdViewer : Effect.void).pipe(
106+
Effect.as(HttpClientResponse.fromWeb(request, new Response(encodeJson(response)))),
96107
);
97108
}),
98109
),
@@ -118,13 +129,31 @@ it.effect("refreshes Forgejo viewer permissions immediately after credentials ch
118129
it.effect("reselects the current tea login between API requests", () => {
119130
let account = "alice";
120131
const selected: string[] = [];
132+
let holdLogin: Effect.Effect<void> = Effect.void;
121133
return Effect.gen(function* () {
122134
const cli = yield* ForgejoCli.make;
123-
const input = { cwd: "/repo", repository: "https://forgejo.test/acme/web", path: "user" };
135+
const input = {
136+
cwd: "/repo\n%work",
137+
repository: "https://forgejo.test/acme/web",
138+
path: "user",
139+
};
124140
assert.strictEqual((yield* cli.api(input)).stdout, '{"login":"alice"}');
125141
account = "bob";
126142
assert.strictEqual((yield* cli.api(input)).stdout, '{"login":"bob"}');
127-
assert.deepStrictEqual(selected, ["alice", "bob"]);
143+
const started = yield* Deferred.make<void>();
144+
const release = yield* Deferred.make<void>();
145+
holdLogin = Deferred.succeed(started, undefined).pipe(Effect.andThen(Deferred.await(release)));
146+
const pending = yield* cli.api(input).pipe(Effect.forkChild);
147+
yield* Deferred.await(started);
148+
account = "carol";
149+
holdLogin = Effect.void;
150+
assert.strictEqual(
151+
(yield* cli.api({ ...input, freshCredentials: true })).stdout,
152+
'{"login":"carol"}',
153+
);
154+
yield* Deferred.succeed(release, undefined);
155+
assert.strictEqual((yield* Fiber.join(pending)).stdout, '{"login":"bob"}');
156+
assert.deepStrictEqual(selected, ["alice", "bob", "carol", "bob"]);
128157
}).pipe(
129158
Effect.provideService(
130159
FileSystem.FileSystem,
@@ -139,19 +168,28 @@ it.effect("reselects the current tea login between API requests", () => {
139168
process: {
140169
run: (input) => {
141170
assert.strictEqual(input.command, "tea");
171+
assert.strictEqual(input.cwd, "/repo\n%work");
142172
if (input.args[0] === "login")
143-
return Effect.succeed(
144-
processOutput(
145-
encodeJson([
146-
{ name: account, url: "https://forgejo.test", user: account, default: "true" },
147-
]),
173+
return holdLogin.pipe(
174+
Effect.as(
175+
processOutput(
176+
encodeJson([
177+
{
178+
name: account,
179+
url: "https://forgejo.test",
180+
user: account,
181+
default: "true",
182+
},
183+
]),
184+
),
148185
),
149186
);
150187
assert.strictEqual(input.args[0], "api");
151-
assert.strictEqual(input.args[input.args.indexOf("--login") + 1], account);
152-
selected.push(account);
188+
const login = input.args[input.args.indexOf("--login") + 1] ?? "";
189+
assert.notStrictEqual(login, "");
190+
selected.push(login);
153191
return Effect.succeed(
154-
processOutput(encodeJson({ login: account }), { stderr: "HTTP/1.1 200 OK\n" }),
192+
processOutput(encodeJson({ login }), { stderr: "HTTP/1.1 200 OK\n" }),
155193
);
156194
},
157195
},
@@ -173,6 +211,7 @@ it.effect("shares pending Forgejo pull reads and refreshes after writes and fail
173211
Layer.mock(ForgejoCli.ForgejoCli)({
174212
api: (input) =>
175213
Effect.gen(function* () {
214+
assert.strictEqual(input.cwd, "/repo\n%work");
176215
paths.push(input.path);
177216
if (input.method === "PATCH") {
178217
state = "closed";
@@ -223,14 +262,9 @@ it.effect("shares pending Forgejo pull reads and refreshes after writes and fail
223262
const summary = provider.getChangeRequestSummary;
224263
const checks = provider.getChangeRequestChecks;
225264
if (!summary || !checks) return yield* Effect.die("narrow reads missing");
226-
const input = { cwd: "/repo", repository: "acme/web", host: "forgejo.test", number: 42 };
265+
const input = { cwd: "/repo\n%work", repository: "acme/web", host: "forgejo.test", number: 42 };
227266
const reads = yield* Effect.all(
228-
[
229-
summary(input),
230-
checks(input),
231-
provider.getViewerPermissions(input),
232-
provider.getChangeRequest(input),
233-
],
267+
[summary(input), checks(input), provider.getChangeRequest(input)],
234268
{ concurrency: "unbounded" },
235269
).pipe(Effect.forkChild);
236270
yield* Deferred.await(started);

0 commit comments

Comments
 (0)